Jump to content

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 />";
      }
   }
?>

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.