How to count files

How to count files in a folder recursively

I needed some script to count files in directories recursively to see how many .jpg files there were spread among an entire account on a server and how much space they are using. Took a few minutes to figure out how it’s done, and i’d like to share the bit of code here in case you are trying to accomplish the same (or something similar). The script posted below has the “scan for .jpg” part removed and now displays the entire amount of files found plus the total size of all files.

This might come in handy for certain hosts like or that have inode limits / file limits in place (10.000 files for and roughly 15.000 files for So if you would like to know how many files there are in your account copy the source code into a text file, name it “count.php” or anything you like (the .php extension however is mandatory), upload it to your account (preferrably your document root) and open it in a browser. Depending on how many files there are it might take a few seconds for the script to complete, and when it’s done it will show you how many files there are in total.

So, here’s the code then:


Be the first to comment

Leave a Reply

Your email address will not be published.

I confirm