Heim >Backend-Entwicklung >PHP-Tutorial >利用rmdir()函数实现PHP递归删除目录_PHP教程
我们大家都知道PHP 是一个小开放源码的一门技术,随着越来越多的人意识到它的实用性从而逐渐发展起来。Rasmus Lerdorf 在 1994 年发布了 PHP 的第一个版本。从那时起它就飞速发展,并在原始发行版上经过无数的改进和完善现在已经发展到版本 4.0.3 。
另一方面,假设您从创建 product.php 页面开始。它没有静态信息,而是编码成可以从产品数据库中提取信息并动态地构建一个页面。然后您就拥有了一个元数据页面,它可以根据存储在数据库中的信息 提供一个、一百个、甚至十万个单独页面。现在网站管理员不再整天都简单重复更新静态页面的工作,因为在更新公司数据库中的信息同时就可以更新页面上的信 息。这样就消除了令人头疼的时间延迟(在数据库中更改信息和在网站上显示信息之间的时间间隔)。下面我们来看一个php递归删除目录的例子,希望对大家有帮助。
rmdir()函数就可以搞定,但是要删除一个非空目录,将不能进行快速的删除,必须先将目录中文件删除,但是目录里可能还会有子目录所以要进行PHP递归删除目录:
PHP递归删除目录代码:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span>?php </span></span><li class=""><span>functiondeletedir($dir){ </span></li> <li class="alt"> <span>if(!</span><span class="attribute">handle</span><span>=@opendir($dir)){//检测要打开目录是否存在 </span> </li></span></li> <li class=""><span>die("没有该目录"); </span></li> <li class="alt"><span>} </span></li> <li class=""> <span>while(false!==($</span><span class="attribute">file</span><span>=</span><span class="attribute-value">readdir</span><span>($handle))){ </span> </li> <li class="alt"><span>if($file!=="."&&$file!==".."){//排除当前目录与父级目录 </span></li> <li class=""> <span>$</span><span class="attribute">file</span><span>=$dir.DIRECTORY_SEPARATOR.$file; </span> </li> <li class="alt"><span>if(is_dir($file)){ </span></li> <li class=""><span>deletedir($file); </span></li> <li class="alt"><span>}else{ </span></li> <li class=""><span>if(@unlink($file)){ </span></li> <li class="alt"> <span>echo"文件</span><span class="tag"><span class="tag-name">b</span><span class="tag">></span><span>$file</span><span class="tag"></span><span class="tag-name">b</span><span class="tag">></span><span>删除成功。</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span></span><li class=""><span>}else{ </span></li> <li class="alt"> <span>echo"文件</span><span class="tag"><span class="tag-name">b</span><span class="tag">></span><span>$file</span><span class="tag"></span><span class="tag-name">b</span><span class="tag">></span><span>删除失败!</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span></span><li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span>if(@rmdir($dir)){ </span></li> <li class=""> <span>echo"目录</span><span class="tag"><span class="tag-name">b</span><span class="tag">></span><span>$dir</span><span class="tag"></span><span class="tag-name">b</span><span class="tag">></span><span>删除成功了。</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>n"; </span></span><li class="alt"><span>}else{ </span></li> <li class=""> <span>echo"目录</span><span class="tag"><span class="tag-name">b</span><span class="tag">></span><span>$dir</span><span class="tag"></span><span class="tag-name">b</span><span class="tag">></span><span>删除失败!</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>n"; </span></span><li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>//测试程序 </span></li> <li class="alt"> <span>$</span><span class="attribute">dir</span><span>=</span><span class="attribute-value">"/var/www/test"</span><span>; </span> </li></span> </li> <li class=""><span>deletedir($dir); </span></li> <li class="alt"> <span>?</span><span class="tag">></span><span> </span> </li></span> </li> <li class=""><span> </span></li> <li class="alt"><span>在/var/www/test文件夹下建一写文件夹和文件测试 </span></li> <li class=""> <span>shell</span><span class="tag">></span><span>touchaaa </span> </li></span> </li> <li class="alt"> <span>shell</span><span class="tag">></span><span>touchbbb </span> </li></span> </li> <li class=""> <span>shell</span><span class="tag">></span><span>touchccc </span> </li> <li class="alt"> <span>shell</span><span class="tag">></span><span>toucheee </span> </li> <li class=""> <span>shell</span><span class="tag">></span><span>touchffff </span> </li> <li class="alt"> <span>shell</span><span class="tag">></span><span>mkdir111 </span> </li> <li class=""> <span>shell</span><span class="tag">></span><span>mkdir222 </span> </li> <li class="alt"> <span>shell</span><span class="tag">></span><span>mkdir333 </span> </li> <li class=""><span>分别再在111,222,333文件夹下建写文件这里就不多说了,然后给他们权限 </span></li> <li class="alt"> <span>shell</span><span class="tag">></span><span>chown[url]www.www[/url]test-R </span> </li> </ol>