Remove all files, folders and their subfolders with php [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

This question already has an answer here:

I need a script which can remove a whole directory with all their subfolders, files and etc. I tried with this function which I found in internet before few months ago but it not work completely.

function deleteFile($dir) {     if(substr($dir, strlen($dir)-1, 1) != '/') {          $dir .= '/';      }     if($handle = opendir($dir)) {          while($obj = readdir($handle)) {              if($obj != '.' && $obj != '..') {                  if(is_dir($dir.$obj)) {                      if(!deleteFile($dir.$obj)) {                         echo $dir.$obj."<br />";                         return false;                     }                 }                 elseif(is_file($dir.$obj)) {                      if(!unlink($dir.$obj)) {                         echo $dir.$obj."<br />";                         return false;                     }                 }             }         }         closedir($handle);          if(!@rmdir($dir)) {             echo $dir.'<br />';             return false;         }         return true;     }     return true; } 

For the test I use a unpacked archive of prestashop and I try to delete the folder where archive is unpacked but it doesn't work.

/home/***/public_html/prestashop/img/p/3/ /home/***/public_html/prestashop/img/p/3 /home/***/public_html/prestashop/img/p /home/***/public_html/prestashop/img 

These are the problem folders. At the first time I think - "May is a problem with the chmod of the files" but when I test with all files chmod permission 755 (after that with 777) - the result was the same.

回答1:

<?php   function rrmdir($dir) {   if (is_dir($dir)) {     $objects = scandir($dir);     foreach ($objects as $object) {       if ($object != "." && $object != "..") {         if (filetype($dir."/".$object) == "dir")             rrmdir($dir."/".$object);          else unlink   ($dir."/".$object);       }     }     reset($objects);     rmdir($dir);   }  } ?> 

Try out the above code from php.net

Worked fine for me



回答2:

You can use a cleaner method for doing a recursive delete of a directory.

Example:

function recursiveRemove($dir) {     $structure = glob(rtrim($dir, "/").'/*');     if (is_array($structure)) {         foreach($structure as $file) {             if (is_dir($file)) recursiveRemove($file);             elseif (is_file($file)) unlink($file);         }     }     rmdir($dir); } 

Usage:

recursiveRemove("test/dir/"); 


回答3:

The easiest and the best way using the system() method

$dir = dirname ( "/log" ); if ($handle = opendir($dir)) {     while (( $file = readdir($handle)) !== false ) {         if ($file != "." && $file != "..") {             system("rm -rf ".escapeshellarg($dir.'/'.$file));         }     } } closedir($handle); 


回答4:

function delete_files($dir) {    foreach(glob($dir . '/*') as $file) {      if(is_dir($file)) delete_files($file); else unlink($file);    } rmdir($dir);  } 


回答5:

/**  * Deletes a directory and all files and folders under it  * @return Null  * @param $dir String Directory Path  */ function rmdir_files($dir) {  $dh = opendir($dir);  if ($dh) {   while($file = readdir($dh)) {    if (!in_array($file, array('.', '..'))) {     if (is_file($dir.$file)) {      unlink($dir.$file);     }     else if (is_dir($dir.$file)) {      rmdir_files($dir.$file);     }    }   }   rmdir($dir);  } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!