欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

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

程序员文章站 2022-06-16 09:27:27
...

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