Posted By

level09 on 08/18/09


Tagged


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

luman
umang_nine
ReeceMarsland


Delete directory including all content


 / Published in: PHP
 

  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  

You need to login to post a comment.