Jump to content

file count in directory question


next

Recommended Posts

	$site_root = $_SERVER['DOCUMENT_ROOT'];
$images_dir = $site_root . "/gallery/images";

$dir_handle = opendir($images_dir);
while($file = readdir($dir_handle)) {
	$fileCount++;
	echo "$fileCount - $file<br />";
}

why are the first 2 listed items are dots??? I have 11 files in a directory, count returns 13:

1 - .

2 - ..

3 - 1.jpg

4 - 3.jpg

5 - 2.jpg

6 - 5.jpg

7 - 7.jpg

8 - 8.jpg

9 - 9.jpg

10 - 10.jpg

11 - 11.jpg

12 - 4.jpg

13 - 6.jpg

 

and is there a better way to count files?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/107931-file-count-in-directory-question/
Share on other sites

i tried

while($file = readdir($dir_handle)) {
if(is_file($file)) {		
$fileCount++;
echo "$fileCount - $file<br />";
}
}

but it returns a null result  ??? .

I pretty much count images, i could use InStr to check, but wondering why is is_file breaking my result.

Any ideas?

The code you provided seems to work just fine for me.  It lists all the files in my directory except the . and ..

 

<?php
   $site_root = $_SERVER['DOCUMENT_ROOT'];
   $images_dir = $site_root; // I took out the path so I could test it on my server

   $dir_handle = opendir($images_dir);
   while($file = readdir($dir_handle)) {
      if(is_file($file)) {	
         $fileCount++;
 echo "$fileCount - $file<br />";
      }
   }
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.