Posted By

fackz on 08/03/09


Tagged

delete dir folder directory


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

fackz
ikimozu
irishsk
umang_nine
BrianCoyDesign
pster


deleting a folder with php


 / Published in: PHP
 

URL: http://aidanlister.com/2004/04/recursively-deleting-a-folder-in-php/

  1. <?php
  2. /**
  3.  * Delete a file, or a folder and its contents (recursive algorithm)
  4.  *
  5.  * @author Aidan Lister <[email protected]>
  6.  * @version 1.0.3
  7.  * @link http://aidanlister.com/repos/v/function.rmdirr.php
  8.  * @param string $dirname Directory to delete
  9.  * @return bool Returns TRUE on success, FALSE on failure
  10.  */
  11. function rmdirr($dirname)
  12. {
  13. // Sanity check
  14. if (!file_exists($dirname)) {
  15. return false;
  16. }
  17.  
  18. // Simple delete for a file
  19. if (is_file($dirname) || is_link($dirname)) {
  20. return unlink($dirname);
  21. }
  22.  
  23. // Loop through the folder
  24. $dir = dir($dirname);
  25. while (false !== $entry = $dir->read()) {
  26. // Skip pointers
  27. if ($entry == '.' || $entry == '..') {
  28. continue;
  29. }
  30.  
  31. // Recurse
  32. rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
  33. }
  34.  
  35. // Clean up
  36. $dir->close();
  37. return rmdir($dirname);
  38. }
  39. ?>

Report this snippet  

You need to login to post a comment.