How do you use PHP4 to count # of files in a directory?
Thanks...
| SitePoint Sponsor |
How do you use PHP4 to count # of files in a directory?
Thanks...





$dir = dir("pathtodir");
while($file = $dir->read()) {
if (($file != ".") && ($file != "..")) {
$filelist[] = $file;
}
}
print count($filelist);
Please don't PM me with questions.
Use the forums, that is what they are here for.
Thanks...what do I put in pathtodir?
I want the program to check for the number of files found in http://www.domain.com/hardware/images/samples/$id





YOU will need to the actual physical path not the http path but the server path for instance if your dir was locate at /home/www/html/stuff/
$dir = dir("/home/www/html/stuff/");
Please don't PM me with questions.
Use the forums, that is what they are here for.
Thanks...
The code given seems to have some problems when the directory doesn't exist. How can I check to see if a directory exist? This way, I won't run into any problems with the codes.
THanks.





You can do this:Originally posted by usbworkshop
Thanks...
The code given seems to have some problems when the directory doesn't exist. How can I check to see if a directory exist? This way, I won't run into any problems with the codes.
THanks.
if(!$dir) {
echo "Error: cannot read directory"; exit;
}
Thanks I got it working.





Or you can use
$d = "/pat/to/some/dir/";
if (is_dir($d)) {
$dir = dir($d);
while($file = $dir->read()) {
if (($file != ".") && ($file != "..")) {
$filelist[] = $file;
}
}
print count($filelist);
}
else {
print "the directory ". $d ." does not exist";
}
Please don't PM me with questions.
Use the forums, that is what they are here for.
Bookmarks