Posted By

luizlopes on 06/04/08


Tagged

folder files unlink rmdir deletefolder removefolder


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

carlosabargues


Recursively Remove Folder


 / Published in: PHP
 

This was copied off wordpress FTP plugin

  1. /**
  2. * Function used to delete a folder.
  3. * @param $path full-path to folder
  4. * @return bool result of deletion
  5. */
  6. function folderDelete($path) {
  7. if (is_dir($path)) {
  8. if (version_compare(PHP_VERSION, '5.0.0') < 0) {
  9. $entries = array();
  10. if ($handle = opendir($path)) {
  11. while (false !== ($file = readdir($handle))) $entries[] = $file;
  12. closedir($handle);
  13. }
  14. }else{
  15. $entries = scandir($path);
  16. if ($entries === false) $entries = array();
  17. }
  18.  
  19. foreach ($entries as $entry) {
  20. if ($entry != '.' && $entry != '..') {
  21. folderDelete($path.'/'.$entry);
  22. }
  23. }
  24.  
  25. return rmdir($path);
  26. }else{
  27. return unlink($path);
  28. }
  29. }

Report this snippet  

You need to login to post a comment.