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

MongoDB\GridFS\Bucket::openDownloadStreamByName()

On this page

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

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

function openDownloadStreamByName(
string $filename,
array $options = []
): resource
$filename : string
The filename of the file to download.
$options : array

An array specifying the desired options.

Name
Type
Description
revision
integer

The revision of the file to retrieve. Files with the same filename will be differentiated by their uploadDate field.

Revision numbers are defined as follows:

  • 0 = the original stored file

  • 1 = the first revision

  • 2 = the second revision

  • etc...

  • -2 = the second most recent revision

  • -1 = the most recent revision

Defaults to -1 (i.e. the most recent revision).

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->selectGridFSBucket();
$stream = fopen('php://temp', 'w+b');
fwrite($stream, "foobar");
rewind($stream);
$bucket->uploadFromStream('filename', $stream);
var_dump(stream_get_contents($bucket->openDownloadStreamByName('filename')));

The output would then resemble:

string(6) "foobar"

Back

openDownloadStream()