I need to get the total number of JPG files in the specified directory, including ALL of its subdirectories. There are no subdirectories.
The structure is as follows:
dir1 /
2 files
subdir 1 /
8 files
total dir1 = 10 files
dir2 /
5 files
subdir 1 /
2 files
subdir 2 /
8 files
total dir2 = 15 files
I have this function, which does not work fine, since it only considers files in the last subdirectory, and the amount is 2 times larger than the actual number of files. (prints 80 if I have 40 files in the last subdirectory)
public function count_files($path) {
global $file_count;
$file_count = 0;
$dir = opendir($path);
if (!$dir) return -1;
while ($file = readdir($dir)) :
if ($file == '.' || $file == '..') continue;
if (is_dir($path . $file)) :
$file_count += $this->count_files($path . "/" . $file);
else :
$file_count++;
endif;
endwhile;
closedir($dir);
return $file_count;
}
source
share