MongoDB\GridFS\Bucket::uploadFromStream()
Definition
Parameters
$filename
: string- The
filename
of the file to create. $source
: resource- Readable stream, from which the new GridFS file's contents will be read.
$options
: arrayAn array specifying the desired options.
NameTypeDescription_idmixedValue to use as the file document identifier. Defaults to a new MongoDB\BSON\ObjectId object.chunkSizeBytesintegerThe chunk size in bytes. Defaults to the bucket'schunkSizeBytes
option.disableMD5booleanWhether to disable automatic MD5 generation when storing files.
Defaults to
false
. Onlytrue
will be supported in 2.0.New in version 1.4.
metadataarray|objectUser data for themetadata
field of the file document. If not specified, themetadata
field will not be set on the file document.
Return Values
The _id
field of the metadata document associated with the newly created
GridFS file. If the _id
option is not specified, a new
MongoDB\BSON\ObjectId object will be used
by default.
Errors/Exceptions
MongoDB\Exception\InvalidArgumentException
for errors related to
the parsing of parameters or options.
MongoDB\Driver\Exception\RuntimeException for other errors at the extension level (e.g. connection errors).
Examples
$bucket = (new MongoDB\Client)->test->selectGridFSBucket(); $stream = fopen('php://temp', 'w+b'); fwrite($stream, "foobar"); rewind($stream); $id = $bucket->uploadFromStream('filename', $stream); var_dump($id);
The output would then resemble:
object(MongoDB\BSON\ObjectId)#3009 (1) { ["oid"]=> string(24) "5acf81017e21e816e538d883" }