Home > php教程 > php手册 > body text

php 递归删除目录(文件夹)

WBOY
Release: 2016-06-13 11:24:32
Original
857 people have browsed it

php 递归删除目录(文件夹)下面举了两个实例,一个是删除单独的空目录代码,一个是批量删除目录文件夹的代码。

php教程 递归删除目录(文件夹)下面举了两个实例,一个是删除单独的空目录代码,一个是批量删除目录文件夹的代码。

//删除单个空文件夹

$dir = 'www.bkjia.com';
if( is_dir( $dir ) ) //判断是否为目录
{
 if( rmdir( $dir ) )
 {
  echo '目录删除成功';
 }
 else
 {
  echo '没有删除目录的权限';
 }
}
else
{
 echo '不是一个有效的目录';
}

//下面来看一个批量递归删除目录的函数吧。

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


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 Recommendations
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!