next Posted May 30, 2008 Share Posted May 30, 2008 $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. Quote Link to comment https://forums.phpfreaks.com/topic/107931-file-count-in-directory-question/ Share on other sites More sharing options...
.josh Posted May 30, 2008 Share Posted May 30, 2008 You can setup a condition in your loop to not count them. p.s.- no there's not really a "better" way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/107931-file-count-in-directory-question/#findComment-553244 Share on other sites More sharing options...
next Posted May 30, 2008 Author Share Posted May 30, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/107931-file-count-in-directory-question/#findComment-553247 Share on other sites More sharing options...
.josh Posted May 30, 2008 Share Posted May 30, 2008 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 />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/107931-file-count-in-directory-question/#findComment-553252 Share on other sites More sharing options...
haku Posted May 30, 2008 Share Posted May 30, 2008 This should work: count(glob("path/to/file/*")); note: If the count is two off, you can use this: count(glob("path/to/file/*")) - 2; Quote Link to comment https://forums.phpfreaks.com/topic/107931-file-count-in-directory-question/#findComment-553255 Share on other sites More sharing options...
mushroom Posted May 30, 2008 Share Posted May 30, 2008 I would use while($file = readdir($dir_handle)) {if($file[0] !=".") { $fileCount++; echo "$fileCount - $file<br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/107931-file-count-in-directory-question/#findComment-553265 Share on other sites More sharing options...
haku Posted May 30, 2008 Share Posted May 30, 2008 This should work: count(glob("path/to/file/*")); I just checked it, and it does. You don't need to use fopen or readdir or anything. Just put the path to the directory you want to check. Quote Link to comment https://forums.phpfreaks.com/topic/107931-file-count-in-directory-question/#findComment-553285 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.