文件夹结构如下:
/files/<user_id>/<filename>.txt
例子:
`/files/15/file1.txt`
`/files/15/file2.txt`
`/files/21/file1.txt`
`/files/23/file1.txt`
我需要计算每个子文件夹中的文件总数,但只在子文件夹级别上。也就是说,如果存在另一个文件夹,比如/files/23/dir/file1.txt
,那么这个文件夹及其内容不应该被计算在内。
输出:
<folder_name>: <folder_count> files
例子:
15: 23 files
21: 2 files
23: 5 files
如何对子目录进行递归计数,但忽略子目录中的目录?
感谢编辑:
我的代码:
<?php
// integer starts at 0 before counting
$i = 0;
$path = '../../../../../../../home/bpn_sftp';
$dirs = glob($path . '/*' , GLOB_ONLYDIR);
foreach($dirs as $dir){
while (($file = readdir($dir)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir($dir.$file))
{
$file_count = count( glob($dir.'*.txt') );
echo $dir." has ".$file_count." files<br>";
$i++;
}
}
}
echo "Total count: ".$i." files";
?>
设法使它与递归文件夹扫描一起工作,将文件计数限制为我正在寻找的文件类型。
<?php
// integer starts at 0 before counting
$i = 0;
$path = './test';
$dirs = glob($path . '/*' , GLOB_ONLYDIR);
foreach($dirs as $dir){
$file_count = count( glob($dir.'/*.txt') );
echo $dir." has ".$file_count." files<br>";
$i++;
}
echo "Total count: ".$i." files";
?>