Soshite, I wrote a pretty decent function a while back to find all of the files of a certain type(s) in a directory. It supports recursion like you need. With some slight modifications you could easily use it to count the images instead of get their names. Or you could just use count() on the array of filenames it returns.
I don’t know how good you are with PHP, so if you only have some basic PHP skills, I’d be glad to modify it for you and give you a function that just counts them for you. Just let me know.
Here is the code. I wrote it to make a simple gallery, so you can remove the code that isn’t part of the function. I’m leaving it in for now so you can get example of how to use it, though. It’s fairly well documented in the comments, but if you need help with it, I’ll be glad to do that, too.
<?php
//gets images; see below for explanation of function use
$files = get_files(array('jpg','png'));
//exports HTML
if(is_array($files)) {
foreach($files as $this_file) {
echo('<img alt="" src="' . $this_file . '" /><br />');
}
}
//*** the get_files function finds of all files of $type (can be array of values or just single value) in $directory (must only be single value, defaults to current folder)
//*** specifiying $case_sensitive makes the types case sensitive or not (default false), so if it's true then 'JPG' will not match 'jpg'
//*** you can specify the depth of folders to include (default is current folder only, or 0); specify -1 for infinite depth
function get_files($type = null, $case_sensitive = false, $depth = 0, $directory = '.') { //gets files of certain type(s) in certain directory
if(is_dir($directory)) { //check if specified directory is directory
if($handle = opendir($directory)) { //open directory and check if open was successful
$files = array(); //set files to empty array
while(($this_file = readdir($handle)) !== false) { //loop through files
if($this_file == '.' || $this_file == '..') { continue; } //check if file is current directory or parent directory and continue
$this_file = $directory . (substr($directory, strlen($directory) - 1, 1) == '/' ? '' : '/') . $this_file; //set file to path of file
if(is_dir($this_file)) { //check if file is directory
if($depth > 0 || $depth == -1) { //check if depth is greater than 0 or infinite
$these_files = array(); //set these files to empty array
$these_files = get_files($type, $case_sensitive, ($depth == -1 ? -1 : $depth - 1), $this_file); //get files in directory
if(is_array($these_files)) { //check if files is array
foreach($these_files as $this2_file) { //loop through files
$files[] = $this2_file; //add file to files
}
}
}
} else { //file is not directory
if(is_null($type)) { //check if type is not specified
$files[] = $this_file; //add file to files
} else { //type is specified
if(!is_array($type)) { //check if type is not array
$type = array($type); //set type to new array with type
}
foreach($type as $this_type) { //loop through types
$expression = '\.' . $this_type . '$';
if($case_sensitive) { //check if types are case sensitive
if(ereg($expression, $this_file)) { //check if file is correct type
$files[] = $this_file; //add file to files
break;
}
} else { //types are not case sensitive
if(eregi($expression, $this_file)) { //check if file is correct type
$files[] = $this_file; //add file to files
break;
}
}
}
}
}
}
closedir($handle); //close directory
return($files); //return files
}
}
return(false); //return false
}
?>
I hope this helps!