SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Count # of files in a directory
-
Jan 25, 2001, 01:07 #1
- Join Date
- Dec 1999
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
How do you use PHP4 to count # of files in a directory?
Thanks...
-
Jan 25, 2001, 01:49 #2
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
$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.
-
Jan 25, 2001, 01:55 #3
- Join Date
- Dec 1999
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
-
Jan 25, 2001, 02:26 #4
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Jan 25, 2001, 04:24 #5
- Join Date
- Dec 1999
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Jan 25, 2001, 07:44 #6
- Join Date
- Jul 1999
- Location
- Chicago
- Posts
- 2,629
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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;
}
-
Jan 25, 2001, 09:26 #7
- Join Date
- Dec 1999
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks I got it working.
-
Jan 25, 2001, 11:57 #8
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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