===================================================
MongoDB\\GridFS\\Bucket::openDownloadStreamByName()
===================================================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

.. phpmethod:: MongoDB\GridFS\Bucket::openDownloadStreamByName()

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

   .. code-block:: php

      function openDownloadStreamByName(
          string $filename,
          array $options = []
      ): resource

Parameters
----------

``$filename`` : string
  The ``filename`` of the file to download.

``$options`` : array
  An array specifying the desired options.

  .. list-table::
     :header-rows: 1
     :widths: 20 20 80

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

Return Values
-------------

A readable stream resource.

Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-gridfs-filenotfoundexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

Examples
--------

.. code-block:: php

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

.. code-block:: none

   string(6) "foobar"

See Also
--------

- :phpmethod:`MongoDB\GridFS\Bucket::downloadToStream()`
- :phpmethod:`MongoDB\GridFS\Bucket::downloadToStreamByName()`
- :phpmethod:`MongoDB\GridFS\Bucket::openDownloadStream()`
