A simple question for newb and my Google-Fu does not allow me. Using PHP, how can you count the number of files in a given directory, including any subdirectories (and any subdirectories they may have, etc.)? for example, if the directory structure looks like this:
/ Dir_A /
/Dir_A/File1.blah
/ Dir_A / Dir_B /
/Dir_A/Dir_B/File2.blah
/Dir_A/Dir_B/File3.blah
/ Dir_A / Dir_B / Dir_C /
/Dir_A/Dir_B/Dir_C/File4.blah
/ Dir_A / Dir_D /
/Dir_A/Dir_D/File5.blah
The script should return from "5" for "./Dir_A".
I combined the following, but this did not quite return the correct answer, and I'm not sure why:
function getFilecount ($ path = '.', $ filecount = 0, $ total = 0) {
$ ignore = array ('cgi-bin', '.', '..', '.DS_Store');
$ dh = @opendir ($ path);
while (false! == ($ file = readdir ($ dh))) {
if (! in_array ($ file, $ ignore)) {
if (is_dir ("$ path / $ file")) {
$ filecount = count (glob ("$ path / $ file /". "*"));
$ total + = $ filecount;
echo $ filecount; / * debugging * /
echo "$ total"; / * debugging * /
echo "$ path / $ file
"; / * debugging * /
getFilecount ("$ path / $ file", $ filecount, $ total);
}
}
}
return $ total;
}
I am very grateful for any help.
php count recursion
user18359
source share