Home >Backend Development >PHP Tutorial >PHP code PHP code that traverses all directories and files in the specified directory

PHP code PHP code that traverses all directories and files in the specified directory

WBOY
WBOYOriginal
2016-07-29 08:47:201038browse

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.'
';
}


2: scandir read Specify the directory to the array

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn