简体   繁体   English

计算目录中的文件,但排除子目录

[英]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. en ..条目,但不是其他目录。 I added a question by adding comment, but got no reply there. 我通过添加评论添加了一个问题,但没有得到答复。 (too old post, i suppose) ( Count how many files in directory php ) (太老的帖子,我想)( 计算目录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. 在php.net上搜索过,但很多这个主题都没有记录。 I did find the SKIP_DOTS fact, but not one letter about how to exclude directories. 我确实找到了SKIP_DOTS事实,但没有找到关于如何排除目录的一封信。

Now my code produces: There were 76849 Files but this includes the subdirectories too. 现在我的代码生成:有76849个文件,但这也包括子目录。

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. 我现在无法使用的第一个答案,因为我必须更新我的PHP版本。 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. 我还注意到,由于第二个答案,我自己的代码不计算子目录中的文件,它只计算子目录的数量,在我的情况下只是4.所以对于速度,我将使用我自己的代码和它的4。 next week i try to update my php-version and will try again. 下周我尝试更新我的php版本,并将再试一次。

Thank you all for your contribution!!! 谢谢大家的贡献!

Thats easy with CallbackFilterIterators ( available since 5.4 ): 这很容易使用CallbackFilterIterators(自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). Symfony的“ Finder ”组件非常灵活,它通过直观的流畅界面查找文件和目录(实际上它是许多SPL组件的包装)。 Almost 30 methods can configure the result . 大约30种方法可以配置结果。 For instance : size , depth , exclude , ignoredotfiles , path , exclude , followLinks ........ An example taken from the documentation : 例如:size,depth,exclude,ignoredotfiles,path,exclude,followLinks ........从文档中获取的示例:

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) . “文件”组件甚至可以用于远程存储的文件(如亚马逊的S3)。 Installation is simple as writing "symfony/finder": "2.3.*@dev" into composer.json file and running a "composer update" CLI command . 将“symfony / finder”:“2.3.*@dev”编写到composer.json文件并运行“composer update”CLI命令时,安装非常简单。 Up to now 1.4 million installations of this component where made , the best evidence of its quality . 到目前为止,该组件的安装量已达到140万次,是其质量的最佳证据。 Many Frameworks/projects use this component behind the scenes . 许多框架/项目在幕后使用此组件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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