Heim  >  Artikel  >  Backend-Entwicklung  >  php递归遍历目录_PHP教程

php递归遍历目录_PHP教程

WBOY
WBOYOriginal
2016-07-14 10:10:491076Durchsuche

class listdir{
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”;
    $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.=”
  • ”.$item.”
  • \n”;
    }
    }
    }
    $this->depth-=1;
    $d->close();
    $this->tostring.=”
\n”;
return $this->tostring;
}
}
$wapdir=”jquery”;
$d=new listdir($wapdir);
echo $d->tostring();
?>

输出结果:


    Directory:jquery
  • jquery-1.3.2.js

  • jquery-1.3.2.min.js

  • jquery-1.3.2-vsdoc2.js

  • test.html

  • common.js


    • Directory:jquery/d
    • common.js

    • jquery-1.3.2.js


 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477448.htmlTechArticle?php class listdir{ var $depth; var $dirname; var $list; var $tostring; function listdir($dir){ $this-dirname=$dir; $this-depth=0; $this-tostring=; } //把结果保存进多维数组...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:php实战第三天_PHP教程Nächster Artikel:php实战第四天_PHP教程