【问题标题】:Count files in directory, but exclude subdirectories计算目录中的文件,但不包括子目录
【发布时间】:2013-12-07 17:05:43
【问题描述】:

我找到了一篇旧帖子,其中包含几乎完美的代码来解决我的问题:计算目录中的(许多)文件。它不包括 . en .. 条目,但不是其他目录。 我通过添加评论添加了一个问题,但那里没有得到回复。 (太旧的帖子,我想) (Count how many files in directory php)

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

在 php.net 上搜索过,但是很多这个主题都没有记录。我确实找到了 SKIP_DOTS 事实,但没有找到关于如何排除目录的一封信。

现在我的代码生成:有 76849 个文件 但这也包括子目录。

如何更改此代码,以便排除我的子目录?

由于一些答案而更新

/**
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

我现在不能使用第一个答案,因为我必须更新我的 PHP 版本。 在测量时间时,第二个答案需要更多的时间来处理。

我还注意到,由于第二个答案,我自己的代码不计算子目录中的文件,它只计算子目录的数量,在我的例子中只有 4 个。 所以为了速度,我将使用我自己的代码和 sbstract 4。 下周我会尝试更新我的 php 版本,然后再试一次。

感谢大家的贡献!!!

【问题讨论】:

    标签: php directory


    【解决方案1】:

    这很容易使用 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));
    

    【讨论】:

    • 我将在更新我的 PHP 版本后立即使用此代码。因此标记为已接受的答案。
    • PHP 太棒了
    【解决方案2】:

    简单得多,假设文件有扩展名而目录没有:

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

    或者过滤掉目录:

    $count = count(array_diff(glob('images/*'), glob('images/*', GLOB_ONLYDIR)));
    

    【讨论】:

    • 代码更容易,但是执行花费了我自己的代码更多的时间,但是第二个代码的一半时间。所以我不会使用此代码,但将其标记为有用。
    【解决方案3】:

    我会这样做:

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

    当您阅读它时,感觉就像是自我记录的代码。

    【讨论】:

    • 记录等于不解释
    • 由于性能较慢,我不会使用此代码,但将其标记为有用
    【解决方案4】:

    Symfony 的“Finder”组件非常灵活,它通过直观流畅的界面查找文件和目录(实际上它是许多 SPL 组件的包装器)。几乎 30 种方法可以配置结果。例如:大小、深度、排除、忽略文件、路径、排除、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";
    }
    

    “文件”组件甚至可以用于远程存储的文件(如亚马逊的 S3)。 安装很简单,只需将 "symfony/finder": "2.3.*@dev" 写入 composer.json 文件并运行 "composer update" CLI 命令。到目前为止,该组件已安装了 140 万台,是其质量的最佳证明。许多Frameworks/projects 在幕后使用这个组件。

    【讨论】:

      【解决方案5】:

      $fi = new FilesystemIterator(DIR . '/images', FilesystemIterator::SKIP_DOTS); printf("有 %d 个文件", iterator_count($fi));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-17
        • 2017-10-08
        • 2015-08-19
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        • 1970-01-01
        • 2015-06-28
        相关资源
        最近更新 更多