2

I found an old post with the almost perfect code for my problem: counting (many) files in a directory. It excludes the . en .. entries, but not other directories. I added a question by adding comment, but got no reply there. (too old post, i suppose) (Count how many files in directory php)

$fi = new FilesystemIterator(images, FilesystemIterator::SKIP_DOTS);
printf("There were %d Files", iterator_count($fi));

Searched at php.net, but a lot of this subject is not documented. I did find the SKIP_DOTS fact, but not one letter about how to exclude directories.

Now my code produces: There were 76849 Files but this includes the subdirectories too.

How do I change this code, so my subdirectories are excluded?

UPDATE BECAUSE OF SOME ANSWERS

/**
PHP version problem, need update first

$files = new FilesystemIterator('images');
$filter= new CallbackFilterIterator($files, function($cur, $key, $iter) {
return $cur->isFile();
});
printf('There were %d Files', iterator_count($filter));
*/


$time0 = time();

$fi = new FilesystemIterator(images, FilesystemIterator::SKIP_DOTS);

$fileCount = 0;
foreach ($fi as $f) {
    if ($f->isFile()) {
        $fileCount++;
    }
}
printf("xThere were %d Files", $fileCount);

$time1 = time();
echo'<br />tijd 1 = '.($time1 - $time0); // outcome 5

echo'<hr />'; 


$fi = new FilesystemIterator(images, FilesystemIterator::SKIP_DOTS);
printf("yThere were %d Files", iterator_count($fi));
$time2 = time();
echo'<br />tijd 2 = '.($time2 - $time1); // outcome: 0

The first answer i cant use now, because i have to update my PHP version. When measuring time, the second answer takes much more time to proces.

I also noted, because of the second answer, my own code does not count files in subdirectories, it only counts the amount of subdirectories, in my case just 4. So for speed, i will use my own code and sbstract 4 of it. next week i try to update my php-version and will try again.

Thank you all for your contribution!!!

5 Answers 5

10

Thats easy with CallbackFilterIterators ( available since 5.4 ):

$files = new FilesystemIterator('images');
$filter= new CallbackFilterIterator($files, function($cur, $key, $iter) {
    return $cur->isFile();
});

printf('There were %d Files', iterator_count($filter));
2
  • I will use this code as soon as i have updated my PHP version. So marked as accepted answer.
    – Terradon
    Dec 7, 2013 at 18:11
  • Awesome is THE PHP
    – Gags
    May 1, 2016 at 18:00
4

Much easier, assuming files have an extension and directories don't:

$count = count(glob('images/*.*'));

Or to filter out directories:

$count = count(array_diff(glob('images/*'), glob('images/*', GLOB_ONLYDIR)));
1
  • 1
    code is easier, but execution cost more time them my own code, but half the time of the second code. So i wont use this code, but marked it as usefull.
    – Terradon
    Dec 7, 2013 at 18:05
1

I would do it as follows:

$fi = new FilesystemIterator(images, FilesystemIterator::SKIP_DOTS);

$fileCount = 0;
foreach ($fi as $f) {
    if ($f->isFile()) {
        $fileCount++;
    }
}
printf("There were %d Files", $fileCount);

It feels like self documenting code when you read it.

2
  • documenting equals not to explaining
    – nihylum
    Dec 7, 2013 at 17:33
  • I wont use this code because of slower performance, but marked it as usefull
    – Terradon
    Dec 7, 2013 at 18:10
0

Symfony's "Finder" component is extremely flexible , it finds files and directories via an intuitive fluent interface (actually it's a wrapper of many SPL components). Almost 30 methods can configure the result . For instance : size , depth , exclude , ignoredotfiles , path , exclude , followLinks ........ An example taken from the documentation :

use Symfony\Component\Finder\Finder ;
$finder = new Finder();
$iterator = $finder
  ->files()
  ->name('*.php')
  ->depth(0)
  ->size('>= 1K')
  ->in(__DIR__);

foreach ($iterator as $file) {
    print $file->getRealpath()."\n";
}

The "File" component can even be used on files stored remotely (like Amazon's S3) . Installation is simple as writing "symfony/finder": "2.3.*@dev" into composer.json file and running a "composer update" CLI command . Up to now 1.4 million installations of this component where made , the best evidence of its quality . Many Frameworks/projects use this component behind the scenes .

-2

$fi = new FilesystemIterator(DIR . '/images', FilesystemIterator::SKIP_DOTS); printf("There were %d Files", iterator_count($fi));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.