简体   繁体   中英

Count files in directory, but exclude subdirectories

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!!!

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));

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)));

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.

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM