Reputation: 1264
Following folder structure:
/files/<user_id>/<filename>.txt
Examples:
`/files/15/file1.txt`
`/files/15/file2.txt`
`/files/21/file1.txt`
`/files/23/file1.txt`
I need to count the total number of files in each subfolder, but only on the subfolder level. Meaning, if there is another folder, like /files/23/dir/file1.txt
, then this folder and its content should not be counted.
Output:
<folder_name>: <folder_count> files
Examples:
15: 23 files
21: 2 files
23: 5 files
How can one do a recursive count for subdirectories, but ignore directories in the subdirectory?
Thanks
Edit:
My code so far:
<?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";
?>
Upvotes: 0
Views: 270
Reputation: 1264
Managed to make it work with a recursive folder scan, limiting the file count to the filetype I am looking for.
<?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";
?>
Upvotes: 2