Posted By

stz184 on 10/09/10


Tagged


Versions (?)

Delete non-empty directory and files with one function


 / Published in: PHP
 

This function can delete both non-empty directories and files.

  1. function my_delete($dir) {
  2. if(is_file($dir)) {
  3. return false;
  4. }
  5. return true;
  6. }
  7. $hdl = opendir($dir);
  8. while($item = readdir($hdl)) {
  9. if($item != "." && $item != "..") {
  10. if(is_file($dir."/".$item)) {
  11. if([email protected]unlink($dir."/".$item)) {
  12. return false;
  13. }
  14. }
  15. else {
  16. my_delete($dir."/".$item);
  17. }
  18. }
  19. }
  20. closedir($hdl);
  21. if(@rmdir($dir)) {
  22. return true;
  23. }
  24. return false;
  25. }

Report this snippet  

You need to login to post a comment.