Home  >  Article  >  Backend Development  >  遍历指定目录下的所有目录和文件的php代码_php技巧

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

WBOY
WBOYOriginal
2016-05-17 09:14:16977browse
复制代码 代码如下:

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