deleting a folder with php


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.