

Maruf scribbled this post.
I’ve just added a new feature to the Team page, where i’ve added a small CSS image gallery. So now you can see multiple images of the awesome BrightCherryteam!
I like everything to be automated so there is very little input from a human to make things work. To help make the thumbnail image gallery completely automated and dynamic, I needed to get a count of how many images there are in each member’s image folder.

So for example, if Harry our Sales Director has 2 images of himself in his images folder, our script will automatically pull out 2 images. If he uploads a new image of himself in his folder, our script will automatically pull out 3 images. In order for that to work, I needed to write a line of PHP code that counts how many image files there are in his image folder each time the page loads.
I’ve seen plenty of PHP code out there that does a file count in a directory. To be honest, most of the code i’ve seen has been bloated- code that is far too long. All you really need is 1 line of code to do a file count.
Here’s the code I used to count how many files there are in a directory:
1 2 3 | $directory = "../images/team/harry/"; $filecount = count(glob("" . $directory . "*.jpg")); $filecount = THE TOTAL COUNT |
In my particular example, it’s only counting files with a jpg extension. You can change that to anything (i.e txt), or completely remove it if you want to count all files. The * is acting as a wildcard.
In the following few days I’ll provide the code for the actual CSS image gallery.











comments
great! nice and simple. thanks!!
I love this! I thought the other code (mostly loops that increment a counter) was bloated, too. Thanks!
Thanks a lot. Finally a piece of PHP that actually works.
Great job.
Thank you very much.. Works great.
Masha Allah …lovely code Thanks …Maruf
Thanks for the code. However I found that when there are 0 files it still returns 1. I fixed it with an if statement:
//Count .jpg
if (glob(“” . $directory . “*.jpg”) != false)
$filecount = count(glob(“” . $directory . “*.jpg”));
else
$filecount = 0;
Maybe this helps some people struggeling with the same issue.
Wow! I can’t count the number of times I’ve been digging around the internet for the same old “bloated” script because I’ve never actually bothered leaning it by heart. If I’d have done my research a little better I would have found this solution years ago! Simple, elegant, and it just plain works!
I needed the a script to return the filenames too, regardless of their extension. This is my take on it… I hope someone finds it helpful:
# code to return an array of file paths from a directory
$filepath = $pathtodirectory . “*”;
$filenames = array();
foreach (glob($filepath) as $filename) {
array_push($filenames , $filename);
}
print_r($filenames);
I learnt that the path can be relative but it shouldn’t have a leading slash. Also the “*” on the end of the filepath catches all files.
Hey Soloman,
I’m not entirely sure why you needed to use array_push there. Regardless, I have already written a post on how you get the files names here: http://www.brightcherry.co.uk/scribbles/2008/11/02/php-list-all-files-in-a-directory/
Hey Pim,
I’ve actually experienced that problem before.
Many thanks for sharing your solution
Great stuff!