Home > Backend Development > PHP Tutorial > PHP code to traverse all directories and files in a specified directory_PHP tutorial

PHP code to traverse all directories and files in a specified directory_PHP tutorial

WBOY
Release: 2016-07-21 15:23:13
Original
875 people have browsed it

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 directory to array
Copy 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.'
';
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324517.htmlTechArticleCopy the code as follows: ?php function listFiles($path){ $result = array(); foreach(glob ($path.'\'."*") as $item){ $result[strtolower($item)] = $item; if(is_dir($item)){ $result...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template