GoLang MongoGridFS::storeUpload

request it (202)
GoLang replacement for PHP's MongoGridFS::storeUpload [edit | history]

Do you know a GoLang replacement for PHP's MongoGridFS::storeUpload? Write it!

PHP MongoGridFS::storeUpload

PHP original manual for MongoGridFS::storeUpload [ show | php.net ]


(PECL mongo >=0.9.0)

MongoGridFS::storeUploadStores an uploaded file in the database


public mixed MongoGridFS::storeUpload ( string $name [, array $metadata ] )



The name of the uploaded file(s) to store. This should correspond to the file field's name attribute in the HTML form.


Other metadata fields to include in the file document.


These fields may also overwrite those that would be created automatically by the driver, as described in the MongoDB core documentation for the » files collection. Some practical use cases for this behavior would be to specify a custom chunkSize or _id for the file.


The filename field will be populated with the client's filename (e.g. $_FILES['foo']['name']).

Return Values

Returns the _id of the saved file document. This will be a generated MongoId unless an _id was explicitly specified in the metadata parameter.


If multiple files are uploaded using the same field name, this method will not return anything; however, the files themselves will still be processed.


Throws MongoGridFSException if there is an error reading the uploaded file(s) or inserting into the chunks or files collections.


Version Description
1.2.5 Changed second parameter to an array of metadata. Pre-1.2.5, the second parameter was an optional string overriding the filename.


Example #1 MongoGridFS::storeUpload() HTML form example

Suppose you have the following HTML form:

<form method="POST" enctype="multipart/form-data">
    <label for="username">Username:</label>
    <input type="text" name="username" id="username" />

    <label for="pic">Please upload a profile picture:</label>
    <input type="file" name="pic" id="pic" />

    <input type="submit" />

If you wanted to store the uploaded image in MongoDB, you could do the following in the script handling the form submission:

= new MongoClient();
$gridfs $m->selectDB('test')->getGridFS();

$gridfs->storeUpload('pic', array('username' => $_POST['username']));

See Also