Count number of files in directory

by kalai 2008-04-25 19:41:36

Code to get PHP to output how many files are present in a file system.
Each user can have number of directories

Example
users/dir/sub1
users/dir/sub1/sub1
users/dir/sub2/sub1/sub2/sub3

Below code will count the number of files present from the provided path

function countFiles($path)
{
$count = 0;
if (is_dir($path))
{
if ($dh = opendir($path))
{
while (($file = readdir($dh)) !== false && $file != "." && $file != ".." )
{
print $path . $file . "<br>";
if (is_dir($path . $file))
{
$count += countFiles($path . $file);
}
else //is file
{
$count++;
}
}
closedir($dh);
}
}
return $count;
}
// usage:
$path = "./users/{$who}/";
print countFiles($path);

Tagged in:

2724
like
0
dislike
0
mail
flag

You must LOGIN to add comments