- Reference >
- MongoDB\GridFS\Bucket Class >
- MongoDB\GridFS\Bucket::uploadFromStream()
MongoDB\GridFS\Bucket::uploadFromStream()
On this page
Definition
-
MongoDB\GridFS\Bucket::uploadFromStream
Creates a new GridFS file and copies the contents of a readable stream to it.
This method has the following parameters:
Parameter Type Description $filename
string The filename
of the file.$source
resource Readable stream, from which the new GridFS file’s contents will be read. $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.disableMD5
boolean Optional. Whether to disable automatic MD5 generation when storing files.
Defaults to
false
.metadata
array|object Optional. User data for the metadata
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 driver level (e.g. connection errors).
Examples
The output would then resemble: