View Full Version : Counting Files Script?
Maverick88
Oct 27th 2007, 10:31 am
Hi,
I am looking for a script or piece of code that counts the images I have in a folder including the images in the subfolders.
I hope someone can help me.
Thanks in advance.
nico_swd
Oct 27th 2007, 10:34 am
Use the script I wrote here: http://forums.digitalpoint.com/showthread.php?t=529171
Together with this:
$dir = 'path/to/files';
$files = preg_grep('~\.(jpe?g|bmp|png|gif)$~i', dir_get_contents($dir));
$num_files = sizeof($files);
echo "There are {$num_files} images.";
Maverick88
Oct 27th 2007, 10:39 am
Wow, that was fast ! Thanks a lot nico_swd !
Maverick88
Oct 27th 2007, 10:59 am
Could you please explain me this piece of code:
$path = "{$dir}/{$entry}";
if (is_file($path))
{
$files[] = $path;
}
else if (is_dir($path) AND $tmp = dir_get_contents($path))
{
$files = array_merge($files, $tmp);
}
nico_swd
Oct 27th 2007, 4:26 pm
If the current entry is a file, it'll be added to the files array (which will be returned at the end). And if the current entry is a directory, the function will call itself again and read the files from this directory and add them to the array as well.
Oh, and please use tags when posting PHP code.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.