Docs Menu
Docs Home
/ / /
PHP Library Manual
/ /

MongoDB\GridFS\Bucket::openDownloadStream()

On this page

  • Definition
  • Parameters
  • Return Values
  • Errors/Exceptions
  • Examples
  • See Also
MongoDB\GridFS\Bucket::openDownloadStream()

Selects a GridFS file by its _id and opens it as a readable stream.

function openDownloadStream($id): resource
$id : mixed
The _id of the file to download.

A readable stream resource.

MongoDB\GridFS\Exception\FileNotFoundException if no file was found for the selection criteria.

MongoDB\Driver\Exception\RuntimeException for other errors at the driver level (e.g. connection errors).

<?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));

The output would then resemble:

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