Navigation
This version of the documentation is archived and no longer supported. To learn how to upgrade your version of PHP Library Manual, refer to the upgrade documentation.

MongoDB\GridFS\Bucket::openDownloadStream()

Definition

MongoDB\GridFS\Bucket::openDownloadStream

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

function openDownloadStream($id): resource

This method has the following parameters:

Parameter Type Description
$id mixed The _id of the file.

Return Values

A readable stream resource.

Errors/Exceptions

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).

Examples

<?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"