views:

357

answers:

4

I am working on a PHP function that will recursively remove all sub-folders that contain no files starting from a given absolute path.

Here is the code developed so far:

function RemoveEmptySubFolders($starting_from_path) {

    // Returns true if the folder contains no files
    function IsEmptyFolder($folder) {
        return (count(array_diff(glob($folder.DIRECTORY_SEPARATOR."*"), Array(".", ".."))) == 0);
    }

    // Cycles thorugh the subfolders of $from_path and
    // returns true if at least one empty folder has been removed
    function DoRemoveEmptyFolders($from_path) {
        if(IsEmptyFolder($from_path)) {
            rmdir($from_path);
            return true;
        }
        else {
            $Dirs = glob($from_path.DIRECTORY_SEPARATOR."*", GLOB_ONLYDIR);
            $ret = false;
            foreach($Dirs as $path) {
                $res = DoRemoveEmptyFolders($path);
                $ret = $ret ? $ret : $res;
            }
            return $ret;
        }
    }

    while (DoRemoveEmptyFolders($starting_from_path)) {}
}

As per my tests this function works, though I would be very delighted to see any ideas for better performing code.

A: 

This line

$ret = $ret ? $ret : $res;

Could be made a little more readable:

$ret = $ret || $res;

Or if PHP has the bitwise operator:

$ret |= $res;
Tom Ritter
A: 

This would spell trouble because calling RemoveEmptySubFolders a few times would probably spell errors because each time you call the function, the other 2 functions are defined again. If they have already been defined, PHP will throw an error saying a function of the same name has already been defined.

Instead try it recursively:

function removeEmptySubfolders($path){

  if(substr($path,-1)!= DIRECTORY_SEPARATOR){
    $path .= DIRECTORY_SEPARATOR;
  }
  $d2 = array('.','..');
  $dirs = array_diff(glob($path.'*', GLOB_ONLYDIR),$d2);
  foreach($dirs as $d){
     removeEmptySubfolders($d);
  }

  if(count(array_diff(glob($path.'*'),$d2))===0){
    rmdir($path);
  }

}

Tested, working nicely. Windows 7 PHP 5.3.0 XAMPP

thephpdeveloper
Well, of course, if the check whether the folder is empty is made after the recursive call, this would be allow for much more efficient algorithm... Shame on me :-|You were right about the issue with nesting functions too, PHP throws fatal error on the second call to the outer function.Thanks a lot!
Dmitry Letano
if it answers, put a tick to it =)
thephpdeveloper
Done. I have to say though that the code proposed by yu_sha seems to better answer my question in terms of performance improvements and I would mark it as an accepted answer, provided that there would be no bugs in the code so it could be copy-pasted.
Dmitry Letano
+1  A: 

If you have empty folder within empty folder within empty folder, you'll need to loop through ALL folders three times. All this, because you go breadth first - test folder BEFORE testing its children. Instead, you should go into child folders before testing if parent is empty, this way one pass will be sufficient.

function RemoveEmptySubFolders($path)
{
  $empty=true;
  foreach (glob($path.DIRECTORY_SEPARATOR."*") as $file)
  {
     if (is_dir($file))
     {
        if (!RemoveEmptySubFolders($file)) $empty=false;
     }
     else
     {
        $empty=false;
     }
  }
  if ($empty) rmdir($path);
  return $empty;
}

By the way, glob does not return . and .. entries.

Shorter version:

function RemoveEmptySubFolders($path)
{
  $empty=true;
  foreach (glob($path.DIRECTORY_SEPARATOR."*") as $file)
  {
     $empty &= is_dir($file) && RemoveEmptySubFolders($file);
  }
  return $empty && rmdir($path);
}
yu_sha
Excellent algorithm!In order for the function to work two small bugs should be corrected:1. $folder should be changed to $path in the foreach statement.2. $file should be passed as a parameter to the recursive function call instead of $starting_from_path.
Dmitry Letano
Thank you. Fixed
yu_sha
A: 

You can try this.

function removeEmptySubfolders($path){

if(substr($path,-1)!= DIRECTORY_SEPARATOR){ $path .= DIRECTORY_SEPARATOR; } $d2 = array('.','..'); $dirs = array_diff(glob($path.'*', GLOB_ONLYDIR),$d2); foreach($dirs as $d){ removeEmptySubfolders($d); }

if(count(array_diff(glob($path.'*'),$d2))===0){ $checkEmpSubDir = explode(DIRECTORY_SEPARATOR,$path); for($i=count($checkEmpSubDir)-1;$i>0;$i--){ $path = substr(str_replace($checkEmpSubDir[$i],"",$path),0,-1);

if(($files = @scandir($path)) && count($files) <= 2){ rmdir($path); } }

}

}

Mike luc