Uploaded image for project: 'PHP Driver: Library'
  1. PHP Driver: Library
  2. PHPLIB-1313

GridFS close writable stream when the process ends to ensure the file is sent

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.18.0
    • Affects Version/s: 1.17.0
    • Component/s: GridFS
    • None

      Running this full PHP script. When the process ends, the file is not written.

      <?php
      
      require __DIR__ . '/../vendor/autoload.php';
      
      $client = new MongoDB\Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
      $database = $client->selectDatabase('test');
      $database->drop();
      $gridfs = $database->selectGridFSBucket();
      
      // Open a stream for writing, similar to fopen with mode 'w'
      $stream = $gridfs->openUploadStream('hello.txt');
      fwrite($stream, 'Hello world!');
      

      To get the file written, the stream must be explicitly closed.

      fclose($stream);

      Note that the stream is correctly closed when this code is wrapped inside a function.

            Assignee:
            jerome.tamarelle@mongodb.com Jérôme Tamarelle
            Reporter:
            jerome.tamarelle@mongodb.com Jérôme Tamarelle
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: