8

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.

Dmitry Letano
  • 85
  • 1
  • 1
  • 4

6 Answers6

35

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);
}

To check hidden files i updated return statement line

    function RemoveEmptySubFolders($path)
    {
        $empty = true;
        foreach (glob($path . DIRECTORY_SEPARATOR . "*") as $file) {
            $empty &= is_dir($file) && RemoveEmptySubFolders($file);
        }
        return $empty && (is_readable($path) && count(scandir($path)) == 2) && rmdir($path);
    }
Raj Kamal
  • 13
  • 4
yu_sha
  • 4,290
  • 22
  • 19
  • 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 Dec 02 '09 at 20:10
  • 1
    Please note that, if your function runs under Linux, hidden sub directories (with names starting with a dot) will also be wrongly matched as "empty" directories. – Werner Feb 06 '13 at 13:26
  • 1
    How could this be modified so that it doesn't delete the root folder being passed? If the original $path contains only empty folders, then it too is removed. No big deal, I'm just curious :) – swl1020 Jun 27 '13 at 14:13
  • 2
    @swl1020, the easiest way would be to change the parameters to RemoveEmptySubFolders($path, $root = true) and then when you call this INSIDE the recursive loop, you would RemoveEmptySubFolder($file, false), and then the return statement should be $empty && (! $root) && rmdir($path); You would call this function as you normally would before. – Mahnster Feb 26 '14 at 21:21
  • 1
    glob($path . DIRECTORY_SEPARATOR . '{,.}[!.,!..]*',GLOB_MARK | GLOB_BRACE) would be better if you want to avoid getting error when there is some files starting example with dot. – Aivar Dec 16 '16 at 15:53
1

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
  • 99,986
  • 30
  • 138
  • 174
1

You can execute a unix command to remove empty directories.

exec("find $starting_from_path -type d -empty -exec rmdir {} \; 2>/dev/null");

grigb
  • 1,151
  • 8
  • 14
  • 1
    Why not just `find $dir -empty -type d -delete`? – Pavel Apr 27 '14 at 05:22
  • No you can not. Because you should NEVER EVER use the "exec" function. It is dangerous and should be disabled in any webserver environment. The only acceptable use is when using php-cli. – Geeklab Dec 07 '14 at 10:26
1

Solution for Linux, using command line tool but faster and simpler than with pure PHP

/**
 * Remove all empty subdirectories
 * @param string $dirPath path to base directory
 * @param bool $deleteBaseDir - Delete also basedir if it is empty
 */
public static function removeEmptyDirs($dirPath, $deleteBaseDir = false) {

    if (stristr($dirPath, "'")) {
        trigger_error('Disallowed character `Single quote` (\') in provided `$dirPath` parameter', E_USER_ERROR);
    }

    if (substr($dirPath, -1) != '/') {
        $dirPath .= '/';
    }

    $modif = $deleteBaseDir ? '' : '*';
    exec("find '".$dirPath."'".$modif." -empty -type d -delete", $out);
}

If you need Windows support, use PHP_OS constant and this one-liner

for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"`enter code here
Pavel
  • 3,967
  • 2
  • 29
  • 35
1

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);
      }
    }
  }
}
Justin
  • 6,611
  • 3
  • 36
  • 57
Mike luc
  • 11
  • 1
0

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

mauris
  • 42,982
  • 15
  • 99
  • 131
  • 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 Dec 02 '09 at 19:32
  • if it answers, put a tick to it =) – mauris Dec 02 '09 at 22:48
  • 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 Dec 03 '09 at 06:38