Home >Backend Development >PHP Tutorial >PHP code PHP code that traverses all directories and files in the specified directory
Copy code The code is as follows:
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.'
';
}
Copy the code The code is as follows:
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.'
';
}
The above introduces the PHP code to traverse all directories and files in the specified directory, including PHP code content. I hope it will be helpful to friends who are interested in PHP tutorials.