check if directories are empty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

check if directories are empty

Post by oboedrew »

I'm writing a script to delete entries for my flat file blog project. The files containing entries are arranged in directories by date. For example: entries/2009/03/02/entry_title.txt. Once an entry has been deleted, I need to check to see if any of the four directories preceding it are now empty. If so, I need to delete them. I know I can delete empty directories with rmdir, but I'm not sure how to check to see if one or more of the directories are empty before deleting them. Any suggestions?

Thanks,
Drew
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: check if directories are empty

Post by ben.artiss »

Hi, this function might help you out a bit:

Code: Select all

function countFiles($dirName,$ext=false) {
    $singleExt = true;
 
    if ($ext && is_array($ext)) {
        $singleExt = false;
    }
 
    if ($dir = opendir($dirName)) {
        $count = 0;
 
        while (false!==($filename = readdir($dir))) {
            if ($filename!="." && $filename!="..") {
                if (!$ext) {
                    $count++;
                } else if ($ext) {
                    $thisExt = getExt($filename);
 
                    if ($singleExt && $thisExt==strtolower($ext)) {
                        $count++;
                    } elseif (!$singleExt) {
                        foreach ($ext as $extension) {
                            if ($thisExt==$extension) {
                                $count++;
                                break;
                            }
                        }
                    }
                }
            }
        }
 
        closedir($dir);
    } else {
        $count = -1;
    }
 
    return $count;
}
Get file extension function is:

Code: Select all

function getExt($filename) {
    return strtolower(end(explode(".", $filename)));
}
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: check if directories are empty

Post by php_east »

use scandir
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: check if directories are empty

Post by ben.artiss »

Of course this may the easier option - I had no idea that existed! :banghead:
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: check if directories are empty

Post by php_east »

i had no idea too until PHP 5 came out.
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

Re: check if directories are empty

Post by oboedrew »

Scandir seems like it ought to work, but I'm having a bit of trouble with it.

if(!scandir($directory)){
rmdir($directory);
}

That ought to work, right? If scandir returns false, the directory should be removed. Except it doesn't do anything. It doesn't give me any error message, but it also doesn't remove the directory. Any suggestions?

Thanks,
Drew
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

Re: check if directories are empty

Post by oboedrew »

if(empty(scandir($directory))){} doesn't work either. I get this: "Fatal error: Can't use function return value in write context."
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: check if directories are empty

Post by papa »

Code: Select all

 
$directory = "/My_dir";
 
$my_dir = scandir($directory);
if(empty($my_dir)){
 
echo "test";
} else echo "not empty";
?>
 
Maybe.
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: check if directories are empty

Post by semlar »

Scandir won't return false unless the directory doesn't exist, you could check this by just printing the contents of scandir($dir). It should always return "." for the current directory and ".." for the parent directory (I have no idea why, it seems redundant - it's a linux thing). Essentially, if you use scandir, you need to check if it returns more than 2 values.
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

Re: check if directories are empty

Post by oboedrew »

Still no luck.

Here's my latest attempt:

$year_contents=scandir("entries/$year/");
$month_contents=scandir("entries/$year/$month/");
$day_contents=scandir("entries/$year/$month/$day/");

if(empty($day_contents)){
rmdir("entries/$year/$month/$day/");
}
if(empty($month_contents)){
rmdir("entries/$year/$month/");
}
if(empty($year_contents)){
rmdir("entries/$year/");
}

Still doesn't work. I'm no longer relying on scandir to return false if the directory is empty. Instead, I'm using scandir to create an array from the contents of the directory, then checking to see if that array is empty. If a directory is empty and I use scandir to return its contents as an array, the result should be an empty array, right?

Drew
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: check if directories are empty

Post by semlar »

I guess I wasn't clear.

Scandir will (almost) ALWAYS return at least TWO values. The array is not going to be empty.

Code: Select all

$year_contents = scandir("./entries/{$year}/");
echo '<pre>';
print_r($year_contents);
echo '</pre>';
echo (count($year_contents) <= 2) ? 'Empty' : 'Not empty';
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

Re: check if directories are empty

Post by oboedrew »

Ah, I see. I misread your earlier post, semlar. That brings up two more questions, and they may be related.

1. You said that scandir will "almost" always return at least two values. Under what circumstances would it return fewer than two values?

2. You said earlier "it's a linux thing." What would happen if I used this script on a site using a non-linux server? Could I still expect scandir to return two entries for an empty directory?

Thanks,
Drew
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

Re: check if directories are empty

Post by oboedrew »

I am officially perplexed. Here's my latest attempt, which again does not work:

Code: Select all

 
$day_contents=scandir("entries/$year/$month/$day/");
if(count($day_contents)<3){
    rmdir("entries/$year/$month/$day/");
}
 
I did the same thing for the year and month directories. It will not delete any of them. Suggestions?

Thanks,
Drew
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: check if directories are empty

Post by BomBas »

1. You have nothing to worry about, the function always returns at least 2 values (path directory etc.)
2. Nah, doesn't matter.

I saw your reply, here is a function I found and upgraded for you:

Code: Select all

function directory_check( $folder )
{
    $files = array();
    if ( $handle = opendir( $folder ) ) {
        while ( false !== ($file = readdir ($handle)) ) {
            if ( $file != "." && $file != ".." ) {
                $files[] = $file;
            }
        }
        closedir( $handle );
    }
    return ( count ($files) > 0 ) ? FALSE : TRUE;
}
Use:

Code: Select all

if( ! directory_check($dir) )
{
// Delete it or what ever..
}
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

Re: check if directories are empty

Post by oboedrew »

Thanks for the function, BomBas. I will give it a try. But meanwhile, any ideas why my earlier attempt at if(count($day_contents)<3) didn't work?

Thanks,
Drew
Post Reply