PHP recursive directory traversal implementation program_PHP tutorial

WBOY
Release: 2016-07-13 16:56:46
Original
843 people have browsed it

PHP itself has a readdir function, but it can only read the current directory. Based on this function, I wrote another function to achieve my needs. The principle of the function is very simple, mainly using recursive calls.

The code is as follows
 代码如下 复制代码

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();
?>

Copy code

class listdir{

var $depth;
var $dirname;

var $list;
代码如下 复制代码

function mkdirs($dir)
{
if(!is_dir($dir))
{
if(!mkdirs(dirname($dir))){
return false;
}
if(!mkdir($dir,0777)){
return false;
}
}
return true;
}
mkdirs('div/css/layout');


同样的思路,php用rmdir和unlink递归删除多级目录的代码:

function rmdirs($dir)
{
$d = dir($dir);
while (false !== ($child = $d->read())){
if($child != '.' && $child != '..'){
if(is_dir($dir.'/'.$child))
rmdirs($dir.'/'.$child);
else unlink($dir.'/'.$child);
}
}
$d->close();
rmdir($dir);
}

var $tostring; function listdir($dir){ $this->dirname=$dir; $this->depth=0;

$this->tostring=””;

}//Save the result into a multi-dimensional array 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; } //Character channeling result 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(); ?> Recursively create multi-level directories 1. First determine whether the bottom directory div/css/layout exists; 2. Determine whether the upper directory div/css of div/css/layout exists. If it does not exist, use div/css as a parameter to proceed recursively The following is the program code:
The code is as follows Copy code
function mkdirs($dir) { if(!is_dir($dir)) { if(!mkdirs(dirname($dir))){ return false; } if(!mkdir($dir,0777)){ return false; } } return true; } mkdirs('div/css/layout'); With the same idea, PHP uses rmdir and unlink to recursively delete multi-level directories: function rmdirs($dir) { $d = dir($dir); while (false !== ($child = $d->read())){ if($child != '.' && $child != '..'){ if(is_dir($dir.'/'.$child)) rmdirs($dir.'/'.$child); else unlink($dir.'/'.$child); } } $d->close(); rmdir($dir); } http://www.bkjia.com/PHPjc/631583.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631583.htmlTechArticlePHP itself has a readdir function, but it can only read the current directory. According to this function, I wrote another function to achieve my needs. The principle of function is very simple...
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!