View Full Version : How to count all files on my website?

03-01-2009, 09:14 PM
Is there a PHP script that can count the total number of files on my website? I need to find out how many files are on my site.


03-01-2009, 11:01 PM
If you are using php5, you could try something like this;


$count = sizeof(scandir('./'));

echo 'There are ' . $count . ' files in this directory on my website.';


03-02-2009, 12:50 AM
That will work for any single directory.
Are you asking how to do it over multiple sub-directories as well?

Oh yeah, and do you count a directory as a file or exclude the count?

03-02-2009, 01:59 AM
Yes, across multiple sub-directories...

03-02-2009, 04:12 AM
You didn't mention if you're using PHP5 or not, but thats ok.
Try this:

$dir = getcwd();
printf("Files in directory %s: %d\n", realpath($dir), countDirectoryFiles($dir));

function countDirectoryFiles($scanDir = null)
$iFileCount = 0;

if (is_file($scanDir))
else if (is_dir($scanDir))
if (false !== ($dh = opendir($scanDir)))
while (false !== ($file = readdir($dh)))
if ($file != '.' && $file != '..')
$iFileCount += countDirectoryFiles($scanDir . DIRECTORY_SEPARATOR . $file);

return $iFileCount;

03-02-2009, 06:14 AM
Tha worked perfectly...thanks...

03-28-2009, 12:35 PM
Is it possible to modify this script so that it counts the folders in a particular directory, the counts jpg files in only one sub folder in the same directory?

I'm still new to php :)

Thank you