For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.

MongoDB\GridFS\Bucket::openDownloadStream()

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 extension level (e.g. connection errors).

<?php $bucket = (new MongoDB\Client)->test->getGridFSBucket(); $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"