Traverses the php code for all directories and files in the specified directory

  • 2020-05-10 17:48:56
  • OfStack

 
<?php 
function listFiles($path){ 
$result = array(); 
foreach(glob($path.'\\'."*") as $item){ 
$result[strtolower($item)] = $item; 
if(is_dir($item)){ 
$result += listFiles($item); 
} 
} 
return $result; 
} 
$path = 'E:\\web\\dianle'; 
foreach(listFiles($path) as $item){ 
echo $item.'<br />'; 
} 

2: scandir reads the specified directory into the array
 
function listFiles($path){ 
$result = array(); 
foreach( scandir($path) as $item ){ 
if($item != '.' && $item != '..' ){ 
$item = $path.'\\'.$item; 
$result[strtolower($item)] = $item; 
if(is_dir($item)){ 
$result += listFiles($item); 
} 
} 
} 
return $result; 
} 
$path = 'E:\\web\\dianle'; 
foreach(listFiles($path) as $item){ 
echo $item.'<br />'; 
} 

Related articles: