I'm working on a simple photo gallery script. One of the features is counting all the images, and I'm sure my current method is not the best way of doing it.
Each photo album is a subfolder, so currently I'm finding folders, then going through each one and counting.
I'm pretty sure there must be another way of doing this, maybe using some kind of recursion, but I haven't figured anything out in the last two weeks. Suggestions?
Each photo album is a subfolder, so currently I'm finding folders, then going through each one and counting.
$i=0; // directories
if ($handle = opendir('./')) {
while (($dir = readdir($handle)) !== false) {
if (is_dir($dir)) {
if ($dir !='.' && $dir !='..') {
$dirs[$i] = $dir;
$i++;
}
}
}
closedir($handle);
}
$i=0;
foreach ($dirs as $dir) { //files in each dir
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false) {
if (is_file("$dir/$file")) {
if (in_array(substr($file, -4), $ext)) {
$images[$i] = $file;
$i++;
}
}
}
closedir($handle);
}
}
return count($images);
I'm pretty sure there must be another way of doing this, maybe using some kind of recursion, but I haven't figured anything out in the last two weeks. Suggestions?
