php递归遍历目录
程序员文章站
2022-06-11 23:42:09
...
class listdir{
var $depth;
var $dirname;
var $list;
var $tostring;
return $this->tostring;
}
}
$wapdir=”jquery”;
$d=new listdir($wapdir);
echo $d->tostring();
?>
var $depth;
var $dirname;
var $list;
var $tostring;
function listdir($dir){
$this->dirname=$dir;
$this->depth=0;
$this->tostring=”";
}
//把结果保存进多维数组
function getlist($dir=”"){
if($dir==”")$dir=$this->dirname;
$d=@dir($dir);
while(false!==($item=$d->read()))
{
if($item!=”.”&&$item!=”..”)
{
$path=$dir.”/”.$item;
if(is_dir($path)){
$this->depth+=1;
$this->getlist($path);
}else{
$this->list[$this->depth][]=$item;
}
}
}
$this->list[$this->depth]['directory']=$dir;
$this->depth-=1;
$d->close();
return $this->list;
}
//字符窜化结果
function tostring($dir=”"){
if($dir==”")$dir=$this->dirname;
$d=@dir($dir);
$this->tostring.=”
- \n”;
- ”.$item.” \n”;
$this->tostring.=”Directory:”.$dir.”\n”;
while(false!==($item=$d->read()))
{
if($item!=”.”&&$item!=”..”)
{
$path=$dir.”/”.$item;
if(is_dir($path)){
$this->depth+=1;
$this->tostring($path);
}else{
$this->tostring.=”
}
}
}
$this->depth-=1;
$d->close();
$this->tostring.=”
return $this->tostring;
}
}
$wapdir=”jquery”;
$d=new listdir($wapdir);
echo $d->tostring();
?>
输出结果:
- jquery-1.3.2.js
- jquery-1.3.2.min.js
- jquery-1.3.2-vsdoc2.js
- test.html
- common.js
- common.js
- jquery-1.3.2.js
Directory:jquery
Directory:jquery/d
上一篇: 对数组进行分组