Home > Backend Development > PHP Tutorial > PHP implements recursive loop for each directory program_PHP tutorial

PHP implements recursive loop for each directory program_PHP tutorial

WBOY
Release: 2016-07-13 10:44:41
Original
796 people have browsed it

To traverse all directories in a folder and list all the files in 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 code is as follows Copy code
 代码如下 复制代码

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

class listdir{
var $depth;
var $dirname;
var $list;
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 results 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();
?>

Deleting an empty directory is very simple ~ one

The rmdir() function can do it, but if you want to delete a non-empty directory, you will not be able to delete it quickly. You must first delete the files in the directory, but there may be subdirectories in the directory, so you need to delete it recursively. The following is mine. Example~

The code is as follows
 代码如下 复制代码
<?php
function deletedir($dir){
if(!handle=@opendir($dir)){ //检测要打开目录是否存在
die("没有该目录");
}
while(false !==($file=readdir($handle))){
if($file!=="."&&$file!==".."){ //排除当前目录与父级目录
$file=$dir .DIRECTORY_SEPARATOR. $file;
if(is_dir($file)){
deletedir($file);
}else{
if(@unlink($file)){
echo "文件$file删除成功。
";
                                  }else{
                                          echo  "文件$file删除失败!
";
                                 }
                }
     }
    if(@rmdir($dir)){
           echo "目录$dir删除成功了。
n";
    }else{
           echo "目录$dir删除失败!
n";
  }
}
 
//测试程序
$dir="/var/www/test";
deletedir($dir);
?>
Copy code
function deletedir($dir){ If(!handle=@opendir($dir)){ //Check whether the directory to be opened exists

die("There is no such directory");

} If($file!=="."&&$file!==".."){ //Exclude the current directory and parent directory $file=$dir .DIRECTORY_SEPARATOR. $file; If(is_dir($file)){                                                                                                                                                  since deletedir($file);                                                                                                                                                      If(@unlink($file)){ echo "File$file was deleted successfully.
";
                                                       Echo "file & lt; b & gt; $ file & lt;/b & gt; delete failure! & Lt; br & gt;";                                                                                                                                                                                                                                                                                 } } If(@rmdir($dir)){                                                                                 echo "directory $dir deleted successfully.
n";
}else{
                                                                                           echo "directory $dir deletion failed!
n"; } }
//Test program $dir="/var/www/test"; deletedir($dir); ? > http://www.bkjia.com/PHPjc/633086.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633086.htmlTechArticleTo traverse all directories in a folder and list all the files in it, PHP itself has one The readdir function can only read the current directory. According to this function, I...
source:php.cn
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