Home > php教程 > php手册 > php递归删除目录

php递归删除目录

WBOY
Release: 2016-06-13 10:33:00
Original
938 people have browsed it

摘自 “linuxer” 博客 http://deidara.blog.51cto.com/400447/118805

要删除一个空的目录很简单~一个
 
rmdir() 函数就可以搞定,但是要删除一个非空目录,将不能进行快速的删除,必须先将目录中文件删除,但是目录里可能还会有子目录所以要进行递归删除~下面是我的例子~


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删除成功了。
";
    }else{
           echo "目录$dir删除失败!
";
  }
}
 
//测试程序
$dir="/var/www/test";
deletedir($dir);
?>


在 /var/www/test 文件夹下建一写 文件夹和文件测试
shell> touch  aaa
shell> touch  bbb
shell> touch  ccc
shell> touch  eee
shell> touch  ffff
shell> mkdir   111
shell> mkdir   222
shell> mkdir   333
分别再在111,222,333  文件夹下建写文件这里就不多说了,然后给他们权限
shell>chown [url]www.www[/url] test -R
然后在IE 打开程序测试吧~~呵呵。
 

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