Navigation
This version of the documentation is archived and no longer supported. To learn how to upgrade your version of PHP Library Manual, refer to the upgrade documentation.

MongoDB\GridFS\Bucket::openUploadStream()

Definition

MongoDB\GridFS\Bucket::openUploadStream

Opens a writable stream for a new GridFS file.

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

This method has the following parameters:

Parameter Type Description
$filename string The filename of the file.
$options array Optional. An array specifying the desired options.

The $options parameter supports the following options:

Option Type Description
_id mixed Optional. Value to use as the file document identifier. Defaults to a new MongoDB\BSON\ObjectId object.
chunkSizeBytes integer Optional. The chunk size in bytes. Defaults to the bucket’s chunkSizeBytes option.
metadata array|object Optional. User data for the metadata field of the file document. If not specified, the metadata field will not be set on the file document.

Return Values

A writable stream resource.

Behavior

Chunk documents will be created as data is written to the writable stream. The metadata document will be created when the writable stream is closed.

Examples

<?php

$bucket = (new MongoDB\Client)->test->selectGridFSBucket();

$uploadStream = $bucket->openUploadStream('filename');
fwrite($uploadStream, 'foobar');
fclose($uploadStream);

$downloadStream = $bucket->openDownloadStreamByName('filename');
var_dump(stream_get_contents($downloadStream));

The output would then resemble:

string(6) "foobar"