【问题标题】:How to get the number of files in the folder recursive in php如何在php中递归获取文件夹中的文件数
【发布时间】:2017-04-07 00:18:42
【问题描述】:

我的网络应用程序中的文件夹结构如下所示。

如何使用 php.ini 递归获取每个文件夹中的文件数。 我正在使用 Codeignaiter。 我不知道如何完成这项任务。

我不能使用 scandir 功能,因为没有物理 路径中的目录。只有文件和文件夹保存在数据库中。

这是我的数据库

请提供任何需要的帮助。 谢谢。

【问题讨论】:

  • 试过什么了吗?
  • 递归目录迭代器或其他一些递归函数?
  • 不,我不知道如何启动它。
  • @Tiger - 不,它不重复我不能使用scandir 函数,因为没有任何路径只能保存数据库中的文件夹和文件。

标签: php recursion directory


【解决方案1】:

通过在其中传递路径作为参数来尝试此功能:

function getFileCount($path) {
        $size = 0;
        $ignore = array('.','..','cgi-bin','.DS_Store');
        $files = scandir($path);
        foreach($files as $t) {
            if(in_array($t, $ignore)) continue;
            if (is_dir(rtrim($path, '/') . '/' . $t)) {
                $size += getFileCount(rtrim($path, '/') . '/' . $t);
            } else {
                $size++;
            }   
        }
        return $size;
    }

【讨论】:

  • 感谢您的努力。但我不能使用它,因为它不是物理目录。我不能使用scandir 函数,因为没有任何目录路径。只能保存在数据库中。
【解决方案2】:

如果您想以递归方式获取项目文件夹中的所有 *.php 文件,您可以使用以下命令:Source

<?php

$Directory = new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);

?>

$Regex 将为每个 PHP 文件包含一个索引数组。

【讨论】:

  • 但问题是我没有任何path只有文件和文件夹保存在数据库中并显示数据库数据。
  • @MenakaKariyawasam 你能告诉你数据库条目吗?
  • 我添加了我的数据库结构。谢谢。
【解决方案3】:

您可以扩展 RecursiveArrayIterator 以获得某种 RecursiveDirectoryIterator 但对于您的虚拟文件系统:

class RecursiveVirtualDirectoryIterator extends RecursiveArrayIterator
{
    private $files;

    public function __construct($parentId, $array = [], $flags = 0)
    {
        $this->files = $array;

        parent::__construct(
            $this->getFilesByParentId($parentId),
            $flags
        );
    }

    private $children;
    public function hasChildren()
    {
        $file = $this->current();

        if ($file['is_file']) {
            return false;
        }

        $this->children = $this->getFilesByParentId($file['id']);

        return !empty($this->children);
    }

    private function getFilesByParentId($id)
    {
        return array_filter($this->files, function ($file) use ($id) {
            return $file['parent_id'] === $id;
        });
    }

    public function getChildren()
    {
        $file = $this->current();
        return new static(
            $file['id'],
            $this->children,
            $this->getFlags()
        );
    }
}

然后,您可以使用 RecursiveIteratorIterator 遍历从数据库返回的数组,并计算最顶层文件夹的文件数:

$iterator = new RecursiveIteratorIterator(
    new RecursiveVirtualDirectoryIterator(0, $files),
    RecursiveIteratorIterator::SELF_FIRST
);

$currentDirectoryName = null;
$filesCount = [];
foreach ($iterator as $file) {
    if ($iterator->getDepth() === 0 && !$file['is_file']) {
        $currentDirectoryName = $file['name'];
        $filesCount[$currentDirectoryName] = 0;
        continue;
    }

    $filesCount[$currentDirectoryName] += 1; 
}

这里是working demo

虽然Standard PHP Library (SPL) 的文档记录很差,但它包含许多有用的东西,可以避免你一遍又一遍地重新发明轮子。

【讨论】:

    猜你喜欢
    • 2011-08-19
    • 2021-12-14
    • 2021-01-12
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 2013-05-15
    • 2018-12-17
    • 1970-01-01
    相关资源
    最近更新 更多