【问题标题】:How to store a directory's content in MongoDB using php?如何使用 php 在 MongoDB 中存储目录的内容?
【发布时间】:2016-08-22 05:49:28
【问题描述】:

我有一个文件夹,里面有文件和文件夹等等。如何使用 PHP 在 mongodb 中存储相同的结构?我希望索引是根文件夹的名称,并且结构类似于文件夹结构。我也希望文件数据按原样存储。

例如:

{

    "Folder1":{
        "Folder2":{ File1:{}
        }
    }

}

【问题讨论】:

    标签: php mongodb laravel


    【解决方案1】:

    this answer的帮助下:

    $structure = fillArrayWithFileNodes(new DirectoryIterator('/path/to/root/'));
    DB::collection('folders')->insert($structure);
    

    fillArrayWithFileNodes 函数:

    function fillArrayWithFileNodes(DirectoryIterator $dir)
    {
      $data = array();
      foreach ( $dir as $node )
      {
        if ( $node->isDir() && !$node->isDot() )
        {
          $data[$node->getFilename()] = fillArrayWithFileNodes( new DirectoryIterator( $node->getPathname() ) );
        }
        else if ( $node->isFile() )
        {
          $data[] = $node->getFilename();
        }
      }
      return $data;
    }
    

    【讨论】:

    • 我正在使用 laravel,但我不断收到 DirectoryIterator::__construct(54117369): failed to open dir: No such file or directory。我将文件夹保存在公用文件夹中。
    猜你喜欢
    • 1970-01-01
    • 2016-09-03
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多