Forum Moderators: coopster

Message Too Old, No Replies

check for folder content

react if empty

         

kumarsena

6:47 pm on Jan 25, 2004 (gmt 0)

10+ Year Member



i ahve the following code that reads a directory and rads the files inside and prints some output. i would like to know how to add a little bit of code so taht in case the directory is empty, it will print empty or something like taht....im sure is prety easy, but cant get my head around it...and also, is that first if statemetn really needed?

<?php

$count = 0;
if ($handle = opendir('receipes'))
{

while (false!== ($file = readdir($handle)))
{

if ($file!= "." && $file!= "..")
{

//read file line by line into array
$lines = file("receipes/$file", "r");

echo "<td>";
echo "<font class=\"subtitle\"><a href=\"print.php3?receipe=$file\">$lines[0]</a></font>";
echo "<br />";
echo "<font class=\"maintext\">$lines[1]</font>";
echo "</td>";
$count = $count +1;

if ($count == 3)
{
echo "</tr><tr>";
$count = 0;
}

}
}

closedir($handle);

}
?>

coopster

1:32 pm on Jan 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



is that first if statement really needed?

Yes, if you intend to read the directory using the PHP readdir() [php.net] function.

You could load the files into an array first, then use a foreach to process if the array is not empty...


$files = array();
if ($handle = @opendir($dir)) {
while (false!== ($filename = readdir($handle))) {
$files[] = $filename;
}
closedir($handle);
if (!$files) {
print "No files in directory";
} else {
natcasesort [php.net]($files); // <-- Want them sorted in natural case order?
foreach ($files as $filename) {
// Eliminates the ., .., as well as any .htaccess files from being listed:
if (is_file($dir.$filename) and substr($filename, 0 , 1)!= '.') {
// your processing code here...
}
}
}
unset($files);
}

kumarsena

3:17 pm on Jan 29, 2004 (gmt 0)

10+ Year Member



thanks,

your code sample there, just pushed me into antther more advanced way of thinking php, hope u dont mind me using ur code...

kumar

hgremminger

2:31 pm on Feb 12, 2004 (gmt 0)



Hi!

I have one question:
can I use such a script for checking a specific folders content? I want the script to send me an email with the filename(s) and a direct link to the file(s).

Any idea?

/Holger