我需要获取指定目录中JPG文件的总数,包括所有它的子目录。没有子子目录。PHP目录中的子目录和子目录函数中的文件总数
结构是这样的:
dir1/ 2 files subdir 1/ 8 files
总DIR1 = 10个文件
dir2/ 5 files subdir 1/ 2 files subdir 2/ 8 files
总DIR2 = 15个文件
我有这个功能,这不因为它只计算最后一个子目录中的文件,并且总数比实际的a多2倍文件的装载。 (将输出80,如果我在最后一个子目录40个文件)
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;
}
谢谢劳伦斯!这工作完美:) – Neoweiter
没有probs。很高兴帮助 –
@Neoweiter也扫描子子目录,我以为你只是想要子目录级别? –