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

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

Re: check if directories are empty

Post by semlar »

Print the contents of $day_contents so you know what the variable actually holds.

Without an error message we have nothing to go on.

The path could be wrong, the if statement might not be firing at all, or rmdir could be failing for some reason.
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

Re: check if directories are empty

Post by oboedrew »

I modified the script and confirmed that scandir($empty_dir) returns an array with two elements: '.' and '..'.

But I sort of have it working now.

Code: Select all

 
$day_contents=count(scandir("entries/$year/$month/$day/"));
$month_contents=count(scandir("entries/$year/$month/"));
$year_contents=count(scandir("entries/$year/"));
if($day_contents<3){
    rmdir("../database/entries/$year/$month/$day/");
}
if($month_contents<3){
    rmdir("../database/entries/$year/$month/");
}
if($year_contents<3){
    rmdir("../database/entries/$year/");
}
 
Strangely, this will delete the $day directory once it has been emptied, but then it will not delete the $month and $year directories, even though they are empty. The method of deletion seems identical to me. Why would it delete one and not another?

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

Re: check if directories are empty

Post by oboedrew »

Problem solved!

The solution was:

Code: Select all

 
$day_contents=scandir("entries/$year/$month/$day/");
$day_contents=count($day_contents);
if($day_contents<3){
    rmdir("entries/$year/$month/$day/");
}
$month_contents=scandir("entries/$year/$month/");
$month_contents=count($month_contents);
if($month_contents<3){
    rmdir("entries/$year/$month/");
}
$year_contents=scandir("entries/$year/");
$year_contents=count($year_contents);
if($year_contents<3){
    rmdir("entries/$year/");
}
 
Thanks again for all the tips!

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

Re: check if directories are empty

Post by oboedrew »

Now, a question of style, as I am entirely self-taught and do not know much about what is or is not considered good style within the community of php developers.

The code above could also be written like this:

Code: Select all

 
if(count(scandir("entries/$year/$month/$day/"))<3){
rmdir("entries/$year/$month/$day/");
}
if(count(scandir("entries/$year/$month/"))<3){
rmdir("entries/$year/$month/");
}
if(count(scandir("entries/$year/"))<3){
rmdir("entries/$year/");
}
 
Clearly this way is shorter, but it seems to me that having multiple nested functions inside a conditional makes for more difficult reading. Is it generally considered preferable to write something the long way that is more quickly understood when you come back to modify it later, or the short way that saves a few lines? Or, is this something on which there is no consensus?

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

Re: check if directories are empty

Post by BomBas »

This way would be better.

Code: Select all

 
// Set all of the variables here
if( count($var) < 3 )
{
rmdir("entries/$year/$month/$day/");
}
if( count($var2) < 3 )
{
rmdir("entries/$year/$month/");
}
Post Reply