Home  >  Article  >  php教程  >  遍历指定目录下的所有目录和文件的php代码

遍历指定目录下的所有目录和文件的php代码

WBOY
WBOYOriginal
2016-06-06 20:39:191049browse

遍历指定目录下的所有目录和文件的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.'
';
}

2: scandir 读取指定目录到数组
代码如下:
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.'
';
}
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