Delete directory including all content


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



Copy this code and paste it in your HTML
  1. function destroyDir($dir, $virtual = false)
  2. {
  3. $ds = DIRECTORY_SEPARATOR;
  4. $dir = $virtual ? realpath($dir) : $dir;
  5. $dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir;
  6. if (is_dir($dir) && $handle = opendir($dir))
  7. {
  8. while ($file = readdir($handle))
  9. {
  10. if ($file == '.' || $file == '..')
  11. {
  12. continue;
  13. }
  14. elseif (is_dir($dir.$ds.$file))
  15. {
  16. destroyDir($dir.$ds.$file);
  17. }
  18. else
  19. {
  20. unlink($dir.$ds.$file);
  21. }
  22. }
  23. closedir($handle);
  24. rmdir($dir);
  25. return true;
  26. }
  27. else
  28. {
  29. return false;
  30. }
  31. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.