文档菜单
文档首页
/ / /
PHP库手册
/ /

MongoDB\GridFS\Bucket::openDownloadStream()

本页内容

  • 定义
  • 参数
  • 返回值
  • 错误/异常
  • 示例
  • 参见
MongoDB\GridFS\Bucket::openDownloadStream()

通过文件的_id选择GridFS文件,并以可读流的形式打开。

function openDownloadStream($id): resource
$id : mixed
要下载的文件的_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"

返回

getWriteConcern()