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.
check if directories are empty
Moderator: General Moderators
Re: check if directories are empty
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.
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
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/");
}
Thanks,
Drew
Re: check if directories are empty
Problem solved!
The solution was:
Thanks again for all the tips!
Cheers,
Drew
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/");
}
Cheers,
Drew
Re: check if directories are empty
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:
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
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/");
}
Thanks,
Drew
Re: check if directories are empty
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/");
}