foochuck Posted June 17, 2008 Share Posted June 17, 2008 I have a folder on my website - I'd like to use PHP to count the number of jpgs in that folder. The folder is called thumbnails. Could someone show me how this is done? Thanks FOO Quote Link to comment https://forums.phpfreaks.com/topic/110609-count-images-in-a-folder/ Share on other sites More sharing options...
wildteen88 Posted June 17, 2008 Share Posted June 17, 2008 Something like <?php $imgCount = 0; foreach (glob("./thumbnails/*.jpg") as $image) { $imgCount++; } echo 'There are ' . $imgCount . ' JPEG\'s in thumbnails/'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/110609-count-images-in-a-folder/#findComment-567460 Share on other sites More sharing options...
foochuck Posted June 17, 2008 Author Share Posted June 17, 2008 Thanks wildteen that works great. I have a couple of questions... 1. "glob() as $image) - could you explain that part to me? 2. Why do I need the period ("./thumbnails/*.jpg") before the thumbnails folder? Thanks again! FOO Quote Link to comment https://forums.phpfreaks.com/topic/110609-count-images-in-a-folder/#findComment-567465 Share on other sites More sharing options...
wildteen88 Posted June 17, 2008 Share Posted June 17, 2008 1. "glob() as $image) - could you explain that part to me? That is the syntax for a foreach loop. glob returns an array of files matching the pattern. Here are links to the manual on foreach and glob 2. Why do I need the period ("./thumbnails/*.jpg") before the thumbnails folder? The . at the beginning of file paths tells PHP to go from the current working directory (where the executing script is). I always tend to add ./ to beginning of file paths, just a habit. You can remove ./ if you want to. Quote Link to comment https://forums.phpfreaks.com/topic/110609-count-images-in-a-folder/#findComment-567475 Share on other sites More sharing options...
foochuck Posted June 17, 2008 Author Share Posted June 17, 2008 Awesome. Thanks wildteen. One other question - could I use this same type of format to not only count the images in that folder, but grab their actual filenames? (boy.jpg, girl.jpg, 1.jpg, DSC0099.jpg, etc)...? Quote Link to comment https://forums.phpfreaks.com/topic/110609-count-images-in-a-folder/#findComment-567482 Share on other sites More sharing options...
wildteen88 Posted June 17, 2008 Share Posted June 17, 2008 Yes. the $image variable holds the filename Note: You cannot use the $image variable outside of the loop. <?php $dir = './thumbnails/' $imgCount = 0; foreach (glob($dir . '*.jpg') as $image) { $imgCount++; echo '<b>Name</b>: ' . $image . '<br />'; echo '<b>Size</b>: ' . filesize($dir . $image) . '<hr />'; } echo 'There are ' . $imgCount . ' JPEG\'s in thumbnails/'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/110609-count-images-in-a-folder/#findComment-567489 Share on other sites More sharing options...
kenrbnsn Posted June 17, 2008 Share Posted June 17, 2008 If you just want the count, you don't need the foreach loop at all: <?php $dir = './thumbnails/' echo 'There are ' . count(glob($dir . '*.jpg')) . " JPEG's in the directory thumnails/"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/110609-count-images-in-a-folder/#findComment-567494 Share on other sites More sharing options...
foochuck Posted June 17, 2008 Author Share Posted June 17, 2008 Thanks kenrbnsn - this one is a lot cleaner. If you just want the count, you don't need the foreach loop at all: <?php $dir = './thumbnails/' echo 'There are ' . count(glob($dir . '*.jpg')) . " JPEG's in the directory thumnails/"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/110609-count-images-in-a-folder/#findComment-567501 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.