Home > Backend Development > PHP Tutorial > Three functions for deleting non-empty directories in PHP

Three functions for deleting non-empty directories in PHP

WBOY
Release: 2016-07-25 08:55:43
Original
892 people have browsed it
  1. function d_rmdir($dirname) { //Delete non-empty directory
  2. if(!is_dir($dirname)) {
  3. return false;
  4. } // bbs.it-home.org
  5. $handle = @opendir($dirname);
  6. while(($file = @readdir($handle)) !== false){
  7. if($file != '.' && $file != '..') {
  8. $dir = $dirname . '/' . $file;
  9. is_dir($dir) ? d_rmdir($dir) : unlink($dir);
  10. }
  11. }
  12. closedir($handle);
  13. return rmdir($ dirname) ;
  14. }
  15. if(d_rmdir("./temp"))
  16. echo "succes";
  17. else
  18. echo "false";
  19. ?>
Copy code

Method 2, found in the PHP manual of.

  1. //Delete non-empty directory
  2. functionremove_directory($dir){
  3.  if($handle=opendir("$dir")){
  4.  while(false!==($item= readdir($handle))){
  5.  if($item!="."&&$item!=".."){
  6.  if(is_dir("$dir/$item")){
  7.   remove_directory("$dir /$item");
  8.   }else{
  9.   unlink("$dir/$item");
  10.   echo"removing$dir/$item
    ";
  11.   }
  12.   
  13.  }
  14.  closedir($handle);
  15. rmdir($dir);
  16.  echo "removing$dir
    ";
  17.  }
  18. }
Copy code

Method 3,

  1. //Function provided by netizens to delete non-empty directories
  2. functionremoveDir($dirName)
  3. {
  4.   if(!is_dir($dirName))
  5.   {
  6.    returnfalse;
  7.   }
  8.   $handle =@opendir($dirName);
  9.  while(($file=@readdir($handle))!==false)
  10.   {
  11.   if($file!='.'&&$file!='..')
  12. {
  13. 〉 dirName);
  14. }
  15. ?>
  16. Copy code
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