PHP遍历文件夹与子目录

我们可以使用的函数有 Scandir,它的作用是列出指定路径中的文件和目录,就像 Dir 一样,

以及更强力的 Glob() 函数,作用是以数组的形式返回与指定模式相匹配的文件名或目录。 

一. 遍历单层文件夹: 

function get_dir_glob(){ 

$tree = array(); 

foreach(glob(‘./*’) as $single){ 

echo $single.”<br/>\r\n”; 

get_dir_glob(); 

二. 递归遍历文件树: 

Glob 函数扫描非常准确,并且会自动排好顺序:

$path = ‘..’; 

function get_filetree($path){ 

$tree = array(); 

foreach(glob($path.’/*’) as $single){ 

if(is_dir($single)){ 

$tree = array_merge($tree,get_filetree($single)); 

else{ 

$tree[] = $single; 

return $tree; 

print_r(get_filetree($path));