PHP- Count Files In A Directory
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 4 5 6 7 8 9 10 | $directory = "../images/team/harry/"; if (glob($directory . "*.jpg") != false) { $filecount = count(glob($directory . "*.jpg")); echo $filecount; } else { echo 0; } |
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.
brightcherry.co.uk
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!
Thanks a lot for ur help…..!!! Can u pls tell me how to count no of files in a directory of different types?
Thanks very much! This is exactly what I was looking for!
Great! +++
I am a new in PHP but i try Your code thanks.
Modified code for directory Listing
<?php
$sno = 1;
echo count(glob('*.*'));
echo " Files”;
foreach(glob(‘*.*’) as $file)
{echo “$sno. $file\n”;$sno++;}
?>
Neat bit of code, thanks
Thanks! Really useful
Just what I was looking for
Hey guys, because i spent a few hours on this, in case some of you has my same problem (to me it kept returning 0 no matter what), it might be something really stupid such as the space before the *
// not working (notice the space before the *)
$numOfPics = count(glob(“” .$dir. ” *.jpg”));
// working fine
$numOfPics = count(glob(“” .$dir. “*.jpg”));
hope this helps someone
Nice Code, really helpful!!
I think this code is doing what I need, but I am not sure how to access the output.
I am using Flash to play a movieclip of images in a directory. I have the Actionscript set up to randomly generate the file number (i.e. slideshow1.jpg, slideshow3.jpg, etc.) I just need the total number of jpgs in a directory to become the value for “var maxVal = ”
I think I can put a script in identifying the output of the php as a variable, then set that variable as the value for maxVal so – var maxVal = VARIABLE_NAME.
Any ideas? Am I even on the right track?
Thank you!
This works perfectly! Thank you!
Just a heads up. Remember to use relative path to the folder and not absolute path when using glob.
Ex
Working:
$directory = “whatever/wp-content/uploads/random/”;
NOT Working
$directory = “http://mydomain.com/whatever/wp-content/uploads/random/”;
cheers
Nice and simple, but anyone know how can we count the sub-directories as well.
Hey,
i edited it to count ALL files in the folder and not pictures
for those who want this code on a function
here it is
you have to define this:
$directory = ”;
if (glob($directory . “*.*”) != false)
{
$filecount = count(glob($directory . “*.*”));
}
else
{
$filecount = ’0×1′;
}
and add this to your index or what ever!
And if the case is that
1. having only 1 directory lets suppose Images
2. Added category and categorized images category wise…
3. Now want to count the images according to their category then what will be the code for this by counting images from 1 directory but for different categories… plz any one help….