Delete non-empty directory and files with one function


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

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


Copy this code and paste it in your HTML
  1. function my_delete($dir) {
  2. if(is_file($dir)) {
  3. if(!@unlink($dir)) {
  4. return false;
  5. }
  6. return true;
  7. }
  8. $hdl = opendir($dir);
  9. while($item = readdir($hdl)) {
  10. if($item != "." && $item != "..") {
  11. if(is_file($dir."/".$item)) {
  12. if(!@unlink($dir."/".$item)) {
  13. return false;
  14. }
  15. }
  16. else {
  17. my_delete($dir."/".$item);
  18. }
  19. }
  20. }
  21. closedir($hdl);
  22. if(@rmdir($dir)) {
  23. return true;
  24. }
  25. return false;
  26. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.