Page 1 of 2
check if directories are empty
Posted: Tue Mar 03, 2009 1:41 pm
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
Re: check if directories are empty
Posted: Tue Mar 03, 2009 2:19 pm
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)));
}
Re: check if directories are empty
Posted: Tue Mar 03, 2009 5:45 pm
by php_east
use scandir
Re: check if directories are empty
Posted: Wed Mar 04, 2009 2:42 am
by ben.artiss
Of course this may the easier option - I had no idea that existed!

Re: check if directories are empty
Posted: Wed Mar 04, 2009 5:56 am
by php_east
i had no idea too until PHP 5 came out.
Re: check if directories are empty
Posted: Wed Mar 04, 2009 9:00 am
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
Re: check if directories are empty
Posted: Wed Mar 04, 2009 9:31 am
by oboedrew
if(empty(scandir($directory))){} doesn't work either. I get this: "Fatal error: Can't use function return value in write context."
Re: check if directories are empty
Posted: Wed Mar 04, 2009 9:58 am
by papa
Code: Select all
$directory = "/My_dir";
$my_dir = scandir($directory);
if(empty($my_dir)){
echo "test";
} else echo "not empty";
?>
Maybe.
Re: check if directories are empty
Posted: Wed Mar 04, 2009 10:06 am
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.
Re: check if directories are empty
Posted: Wed Mar 04, 2009 10:21 am
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
Re: check if directories are empty
Posted: Wed Mar 04, 2009 11:54 am
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';
Re: check if directories are empty
Posted: Wed Mar 04, 2009 12:34 pm
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
Re: check if directories are empty
Posted: Wed Mar 04, 2009 1:05 pm
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
Re: check if directories are empty
Posted: Wed Mar 04, 2009 1:07 pm
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..
}
Re: check if directories are empty
Posted: Wed Mar 04, 2009 2:07 pm
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