Docs 菜单

Docs 主页PHP 库手册

MongoDB\GridFS\Bucket::openDownloadStream()

在此页面上

  • 定义
  • 参数
  • 返回值
  • 错误/异常
  • 举例
  • 另请参阅
MongoDB\GridFS\Bucket::openDownloadStream()

通过_id选择 GridFS 文件并将其作为可读流打开。

function openDownloadStream($id): resource
$id :混合
要下载的文件的_id

可读的流资源。

MongoDB\GridFS\Exception\FileNotFoundException 如果未找到符合选择条件的文件。

MongoDB\Driver\Exception\RuntimeException 驱动程序级别的其他错误(例如连接错误)。

<?php
$bucket = (new MongoDB\Client)->test->selectGridFSBucket();
$uploadStream = fopen('php://temp', 'w+b');
fwrite($uploadStream, "foobar");
rewind($uploadStream);
$id = $bucket->uploadFromStream('filename', $uploadStream);
$downloadStream = $bucket->openDownloadStream($id);
var_dump(stream_get_contents($downloadStream));

而输出将类似如下所示:

string(6) "foobar"
← MongoDB\GridFS\Bucket::getWriteConcern()