Recursively Remove Folder


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

This was copied off wordpress FTP plugin


Copy this code and paste it in your HTML
  1. /**
  2. * Function used to delete a folder.
  3. * @param $path full-path to folder
  4. * @return bool result of deletion
  5. */
  6. function folderDelete($path) {
  7. if (is_dir($path)) {
  8. if (version_compare(PHP_VERSION, '5.0.0') < 0) {
  9. $entries = array();
  10. if ($handle = opendir($path)) {
  11. while (false !== ($file = readdir($handle))) $entries[] = $file;
  12. closedir($handle);
  13. }
  14. }else{
  15. $entries = scandir($path);
  16. if ($entries === false) $entries = array();
  17. }
  18.  
  19. foreach ($entries as $entry) {
  20. if ($entry != '.' && $entry != '..') {
  21. folderDelete($path.'/'.$entry);
  22. }
  23. }
  24.  
  25. return rmdir($path);
  26. }else{
  27. return unlink($path);
  28. }
  29. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.