Posted By

eden159 on 01/16/09


Tagged

php delete folder recursive remove directory directories


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

fackz
umang_nine
Mithun
jeffery2k2610


Delete recursively specific folder (directories) with php


 / Published in: PHP
 

Helpfull function to remove recursively specific folders.

  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  

You need to login to post a comment.