当前位置:首页 > PHP教程 > PHP高级教程

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

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!