For AI agents: a documentation index is available at https://www.mongodb.com/ja-jp/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.

MongoDB\GridFS\Bucket::openUploadStream()

MongoDB\GridFS\Bucket::openUploadStream()

Opens a writable stream for a new GridFS file.

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

An array specifying the desired options.

Name
Type
Description

_id

mixed

Value to use as the file document identifier. Defaults to a new MongoDB\BSON\ObjectId object.

chunkSizeBytes

integer

The chunk size in bytes. Defaults to the bucket's chunkSizeBytes option.

metadata

array|object

User data for the metadata field of the file document. If not specified, the metadata field will not be set on the file document.

A writable stream resource.

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.

<?php $bucket = (new MongoDB\Client)->test->getGridFSBucket(); $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"