Re: Counting files in directory and subdirectory!
| From: | David Tulloh | Date: | Thu, 26 Jan 2006 04:54:29 +0000 |
| Subject: | Re: Counting files in directory and subdirectory! | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-229293@lists.php.net to get a copy of this message | ||
That is some nasty code...
Anything that has repitition like that needs a substantial rewrite, but
I'm guessing that you know that, hence the e-mail.
I just knocked up the following function, powered by the magic of
recursion. It should be close to what you were trying to do.
function count_files($path) {
$number_of_files = 0;
foreach(scandir($path) as $dir) {
if($dir{0} == '.') continue; # Ignore hidden files
if(is_dir($path.'/'.$dir))
$number_of_files += count_files($path.'/'.$dir);
else
$number_of_files++;
}
return $number_of_files;
}
echo count_files("amrs");
David
Nicholas Couloute wrote:
> here is what I tried but it doesn't work! any advice suggestions or
> something?
>
> $count = 0;
> $dir = "/amrs";
> $files1 = scandir($dir);
> if ($files1 !== '.' && $files1 !== '..') {
> foreach ($files1 as $files2){
> $dir2 = "/amrs/$files2";
> $files3 = scandir($dir2);
> if ($files3 !== '.' && $files3 !== '..') {
> foreach ($files3 as $files4){
> $dir3 = "/amrs/$files2/$files4";
> $files5 = scandir($dir3);
> if ($files5 !== '.' && $files5 !== '..') {
> foreach ($files5 as $files6){
> $count++;
> }
> }
> }
> }
> }
> }
> echo "$count";
> ~Nick Couloute
> co-owner/Web Designer
> Sidekick2Music.Com
>