How do I get the file count and total mb used in a directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
3dron
Forum Commoner
Posts: 40
Joined: Sat Feb 01, 2003 10:25 pm

How do I get the file count and total mb used in a directory

Post by 3dron »

I am currently using " while(($file = $cldir->read()) !== false) " with a "++$filecount" to get me the total number of files in each directory.

Is there a better way?

Also how do I get the total file size of all the files in a directory?

THanks

Ron
Rob the R
Forum Contributor
Posts: 128
Joined: Wed Nov 06, 2002 2:25 pm
Location: Houston

Post by Rob the R »

This is a fine way to count the number of files in a directory. To find out the total size of all the files in that directory, use the filesize function in the while loop:

Code: Select all

$totalsize += filesize($file) ;
Just be careful that this will not count the size of the files contained in a subdirectory of the directory you specify. There is an example of a recursive function that calculates the total size of all the files contained in a directory and its subdirectories here:
http://www.php.net/manual/en/function.d ... .php#15203
Post Reply