すべてのサブディレクトリを含む、指定されたディレクトリ内の JPG ファイルの総数を取得する必要があります。サブサブディレクトリはありません。
構造は次のようになります。
dir1/
2ファイル
サブディレクトリ 1/
8ファイル
合計dir1 = 10 ファイル
dir2/
5ファイル
サブディレクトリ 1/
2ファイル
サブディレクトリ 2/
8ファイル
合計dir2 = 15 ファイル
私はこの関数を持っていますが、最後のサブディレクトリ内のファイルのみをカウントし、合計が実際のファイル量の 2 倍になるため、うまく機能しません。(最後のサブディレクトリに40個のファイルがある場合、80を出力します)
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;
}