Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove only empty folders and subfolders in PHP

Tags:

directory

php

I've got a folder, filled with both empty and full folders.

I want to iterate over all of them, figure out if they are empty, and if so, remove them. I've seen some questions that are appliccable, but I cannot figure out the total solution:

  • How can use PHP to check if a directory is empty?
  • Finding empty folders recursively and delete them recursively
  • PHP script to loop through all of the files in a directory?

There must be some easy sure-fire way to do this, using the new PHP5 functions?

Something like (pseudocode full of errors)

<?php
$dir = new DirectoryIterator('/userfiles/images/models/');
foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {
         if(!(new \FilesystemIterator($fileinfo))->valid()) {
              rmdir($fileinfo);
         }
    }
}
?>
like image 308
chocolata Avatar asked Feb 10 '23 20:02

chocolata


1 Answers

This should work for you:

Here I just get all directory's from a specific path with glob() (PHP 4 >= 4.3.0, PHP 5). Then I iterate through every directory and check if it is empty.

If it is empty I remove it with rmdir() else I check if there is another direcotry in it and call the function with the new directory

<?php

    function removeEmptyDirs($path, $checkUpdated = false, $report = false) {
        $dirs = glob($path . "/*", GLOB_ONLYDIR);

        foreach($dirs as $dir) {
            $files = glob($dir . "/*");
            $innerDirs = glob($dir . "/*", GLOB_ONLYDIR);
            if(empty($files)) {
                if(!rmdir($dir))
                    echo "Err: " . $dir . "<br />";
               elseif($report)
                    echo $dir . " - removed!" . "<br />";
            } elseif(!empty($innerDirs)) {
                removeEmptyDirs($dir, $checkUpdated, $report);
                if($checkUpdated)
                    removeEmptyDirs($path, $checkUpdated, $report);
            }
        }

    }


?>

removeEmptyDirs

(PHP 4 >= 4.3.3, PHP 5)
removeEmptyDirs — Removes empty directory's

void removeEmptyDirs( string $path [, bool $checkUpdated = false [, bool $report = false ]] )

Description

The removeEmptyDirs() function goes through a directory and removes every empty directory

Parameters

path
  The Path where it should remove empty directorys

checkUpdated
  If it is set to TRUE it goes through each directory again if one directory got removed

report
  If it is set to TRUE the function outputs which directory get's removed

Return Values

None

As an example:

If $checkUpdated is TRUE structures like this get's deleted entirely:

- dir
   | - file.txt
   | - dir
        | - dir

Result:

- dir
   | - file.txt

If it is FALSE like in default the result would be:

- dir
   | - file.txt
   | - dir  //See here this is still here

If $report is TRUE you get a output like this:

test/a - removed!
test/b - removed!
test/c - removed!

Else you get no output

like image 172
Rizier123 Avatar answered Feb 27 '23 02:02

Rizier123