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


  本文标签:遍历目录,遍历文件

复制代码 代码如下:

<?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.<br />;
}

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.<br />;
}