Docs Menu

Docs HomePHP Library Manual

MongoDB\GridFS\Bucket::downloadToStreamByName()

On this page

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

Selects a GridFS file by its filename and copies its contents to a writable stream.

function downloadToStreamByName(
string $filename,
resource $destination,
array $options = []
): void
$filename : string
The filename of the file to download.
$destination : resource
Writable stream, to which the GridFS file's contents will be written.
$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).

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

MongoDB\Exception\InvalidArgumentException for errors related to the parsing of parameters or options.

MongoDB\Driver\Exception\RuntimeException for other errors at the driver 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);
$destination = fopen('php://temp', 'w+b');
$bucket->downloadToStreamByName('filename', $destination);
var_dump(stream_get_contents($destination, -1, 0));

The output would then resemble:

string(6) "foobar"
←  MongoDB\GridFS\Bucket::downloadToStream()MongoDB\GridFS\Bucket::drop() →