Delete recursively specific folder (directories) with php


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

Helpfull function to remove recursively specific folders.


Copy this code and paste it in your HTML
  1. function removeDirs($path, $dirName, $delete = false) {
  2. $dh = opendir($path);
  3. while ($file = readdir($dh)) {
  4. if($file != '.' && $file != '..') {
  5. $fullpath = $path.'/'.$file;
  6. if(!is_dir($fullpath)) {
  7. if ( $delete ) {
  8. @unlink($fullpath);
  9. }
  10. } else {
  11. if ( $file == $dirName ) {
  12. removeDirs($fullpath, $dirName, true);
  13. @rmdir($fullpath);
  14. } else {
  15. removeDirs($fullpath, $dirName, $delete);
  16. }
  17. if ( $delete ) {
  18. @rmdir($fullpath);
  19. }
  20. }
  21. }
  22. }
  23.  
  24. closedir($dh);
  25. }
  26.  
  27. //example call
  28. removeDirs('/var/www/temp', '.svn');

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.