MongoGridFS::storeUpload
(PECL mongo >=0.9.0)
MongoGridFS::storeUpload — Stores an uploaded file in the database
Description
Parameters
-
name
-
The name of the uploaded file(s) to store. This should correspond to the file field's name attribute in the HTML form.
-
metadata
-
Other metadata fields to include in the file document.
Note:
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.
Note:
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.
Note:
If multiple files are uploaded using the same field name, this method will not return anything; however, the files themselves will still be processed.
Errors/Exceptions
Throws MongoGridFSException if there is an error reading the uploaded file(s) or inserting into the chunks or files collections.
Changelog
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. |
Examples
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" /> </form>
If you wanted to store the uploaded image in MongoDB, you could do the following in the script handling the form submission:
<?php
$m = new MongoClient();
$gridfs = $m->selectDB('test')->getGridFS();
$gridfs->storeUpload('pic', array('username' => $_POST['username']));
?>
See Also
- MongoGridFS::put() - Stores a file in the database
- MongoGridFS::storeBytes() - Stores a string of bytes in the database
- MongoGridFS::storeFile() - Stores a file in the database
- MongoDB core docs on » GridFS
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-mongogridfs.storeupload.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.