PHP- Count Files In A Directory

Image frame
PHP- Count Files In A Directory
Maruf
Sep 6th, 2008
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.

Count files

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.


Filed away: PHP

comments

Image frame
1

great! nice and simple. thanks!!

PoZel
Nov 6th, 2008
Image frame
2

I love this! I thought the other code (mostly loops that increment a counter) was bloated, too. Thanks!

Krista Ehlers
Feb 10th, 2009
Image frame
3

Thanks a lot. Finally a piece of PHP that actually works.

Great job. :)

Nike
Apr 18th, 2009
Image frame
4

Thank you very much.. Works great. :)

humble
Sep 16th, 2009
Image frame
5

Masha Allah …lovely code Thanks …Maruf

shiva
Jan 13th, 2010
Image frame
6

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.

Pim
Feb 3rd, 2010
Image frame
7

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.

Solomon
Feb 18th, 2010
Image frame
8

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/

:)

Maruf
Feb 18th, 2010
Image frame
9

Hey Pim,

I’ve actually experienced that problem before.

Many thanks for sharing your solution :)

Great stuff!

Maruf
Feb 18th, 2010

feel free to leave a scribble

Name:
Email:
gravatar
What an image next to your comments?
visit gravatar.com
Message:
want a website, do ya?

latest projects

Image frame
RG Active
view site- RG Active
Image frame
Property Investment Project
view site- Property Investment Project

latest scribbles

latest comments

zurin Gravatar frame
zurin said
hi, i’m having problem with this function. it works fine...
Karlos Gravatar frame
Karlos said
Awesome awesome awesome :) Just started re-designing...
satheesh Gravatar frame
satheesh said
It solved my problem. Thank you. Is it possible to get the...

blog categories

Get a free quote