//遍历目录及目录下的所有文件和文件夹,返回文件数组
$files=array();
function listAllFiles($dir="D:/www_local/CI/"){
if(is_dir($dir)){
if($handle=opendir($dir)){
//var_dump($handle);resource(2) of type (stream)
while(false!==($file=readdir($handle))){
//var_dump($file);//全是文件名,第1个是点,第2个是点点,其他就abc.php
if($file!="."&&$file!=".."){
//继续分别判断是文件夹还是文件
if(is_dir($dir."/".$file)){
//echo "here"."<br/>";
//如果是文件夹,继续遍历
$files[$file]=listAllFiles($dir."/".$file);
}else{
//如果是文件,添加到文件数组中,记得加上路径
$files[]=$dir."/".$file;
}
}
}
closedir($handle); //遍历完毕,必须关毕
}
}
return $files;
}
echo "<pre>";
print_r(listAllFiles());
echo "</pre>";
php_遍历目录_返回文件数组
最新推荐文章于 2022-02-24 08:46:03 发布