php获取目录下文件夹列表(递归)
获取目录下所有子目录的文件夹列表(递归函数返回的是路径的全称)
应用案例
$url="D:/wwwroot/ytecn.com/ui/"; $list=RemoveMoreSpaces($url); print_r($list);
输出结果为:
Array ( [0] => D:/wwwroot/ytecn.com/ui/code/list [1] => D:/wwwroot/ytecn.com/ui/code [2] => D:/wwwroot/ytecn.com/ui/css [3] => D:/wwwroot/ytecn.com/ui/img [4] => D:/wwwroot/ytecn.com/ui/js [5] => D:/wwwroot/ytecn.com/ui/tinymce/langs [6] => D:/wwwroot/ytecn.com/ui/tinymce/skins [7] => D:/wwwroot/ytecn.com/ui/tinymce )
函数
function GetDirsInDir_Recursive($dir) { $dirs = array(); if (!file_exists($dir)) { return array(); } if (!is_dir($dir)) { return array(); } $dir = str_replace('\\', '/', $dir); if (substr($dir, -1) !== '/') { $dir .= '/'; } if (function_exists('scandir')) { foreach (scandir($dir, 0) as $d) { if (is_dir($dir . $d)) { if (($d != '.') && ($d != '..')) { $array = GetDirsInDir($dir . $d); if (count($array) > 0) { foreach ($array as $key => $value) { $dirs[] = $dir . $d . '/' . $value; } } $dirs[] = $dir . $d; } } } } else { $handle = opendir($dir); if ($handle) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if (is_dir($dir . $file)) { $array = GetDirsInDir($dir . $file); if (count($array) > 0) { foreach ($array as $key => $value) { $dirs[] = $dir . $file . '/' . $value; } } $dirs[] = $dir . $file; } } } closedir($handle); } } return $dirs; }