Eliminar directorio con ficheros dentro


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

Delete a directory including its contents.


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.