您的位置:首页 > 编程语言 > PHP开发

php列出"目录下文件清单"的函数

2012-09-25 12:36 519 查看
虽然挺简单的功能需求,但是昨天晚上还是弄了一会,都怪旁边一直有人跟我说话(唉,找点外在因素安慰自己一下)。

$filespath = array();
function getFilesPath($baseDir){
global  $filespath;
if (is_dir($baseDir)) {
if ($dh = opendir($baseDir)) {
while (($file = readdir($dh)) !== false) {
if( $file == '.'    ||   $file == '..' )continue;
$path = $baseDir . $file;
$filetype = filetype($path);
if( $filetype == 'file'){
if(preg_match('/^\d{4}-\d{2}-\d{2}\.log|txt$/i',$file) ){
$filespath[]=$path;
}
}elseif($filetype == 'dir'){
getFilesPath($path.'/');
}
}
closedir($dh);
}
}else{
echo 'is not a Dir!';
exit;
}
}

getFilesPath($baseDir);

var_dump($filesPath);


解释一下,我的目的是要列出一个目录下(及目录的目录下的[重复吧]......,专业词叫递归)的所有 2012-12-12.log 或 2012-12-12.txt 形式的文件路径。加到一个$filespath的数组内保存起来。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐