Forum Moderators: coopster

Message Too Old, No Replies

counting files in a directory and doing something if there is/isnt

         

surrealillusions

11:15 pm on Dec 13, 2008 (gmt 0)

10+ Year Member



I'm basically trying to see if the thumbs folder has not got any files in it, then some thumbnails need to be created, and if there is some files in there, then the thumbs have already been created and so dont need doing again.

This is the script i have at the moment, but, it keeps re-generating the thumbs each time the script is run. I find out by the last modified date/time on the thumbnail files in testing, and it always says the time that i ran the page. The 'already done' echo bit isnt needed for the final script, but in there so i know that the else statement is used instead of the thumbs been generated, and of course, the 'already done' statement never appears.

Ive tried numerous attempts from scripts on php.net and a quick google search, but they either do the same or throw up errors...

$dir = "thumbs/";
$count = 0;
if(is_dir($dir)) {
if($handle = opendir($dir)) {
while(($file = readdir($handle)) !== false) {
$count++;
}
closedir($handle);
}
}
if ($count == 0) {
include ("thumbnail_generator.php");
}
else {
echo "already done";
}

cameraman

3:28 am on Dec 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might try an absolute path or adding some else's with echos - if it's not [trying to] open the directory you think, it could be failing because 'thumbs/' isn't a directory or isn't opening.

surrealillusions

10:15 pm on Dec 14, 2008 (gmt 0)

10+ Year Member



Tried this..and with or without files in the thumbs folder, and the $count is always 0, well..whatever the $count = 0; line is basically.

So i dont think its reading the folder, which definatly exists, or counting the files correctly.

$thumbsDirectory = "thumbs";
$count = 0;
if(is_dir($thumbsDirectory)) {
if($handle = opendir($thumbsDirectory)) {
while(($file = readdir($handle)) !== false) {
$count++;
}
closedir($handle);
}
}
echo $count;

cameraman

11:20 pm on Dec 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



By absolute path I didn't mean change the name of the variable, I meant something along the lines of
$dir = "home/users/me/public_html/thumbs";

By else's with echos I meant check for the false of each of your conditions, not remove the single else that you had.

$thumbsDirectory = "path/to/my/webdocuments/thumbs";
$count = 0;
if(is_dir($thumbsDirectory)) {
if($handle = opendir($thumbsDirectory)) {
while(($file = readdir($handle)) !== false) {
$count++;
} // endwhile getting files
closedir($handle);
} // endIf opened directory
else echo "opendir() failed.";
} // endIf is directory
else echo "Directory doesn't exist.";
echo $count;

In order to fix a problem you have to figure out exactly where the problem is occurring - you have three things going on up there so you need to narrow it down. Without inline debugging, about the only way is to echo/dump variables and function results all over the place, then remove them and design more elegant ways of handling/reporting/recovering from all remaining possible error conditions.

Just this morning I copied an app from one web site to another and it burped in a particular section - the complaint was invalid argument to implode. Adding echos revealed that it was actually a missing field in a database table 20+ lines earlier. If I hadn't echoed the sql that the script compiled I'd probably still be scratching my head...