Php/docs/mongogridfs.storeupload
MongoGridFS::storeUpload
(PECL mongo >=0.9.0)
MongoGridFS::storeUpload — Stores an uploaded file in the database
说明
public MongoGridFS::storeUpload
( string $name
[, array $metadata
] ) : mixed
参数
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']
).
返回值
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.
错误/异常
Throws MongoGridFSException if there is an error
reading the uploaded file(s) or inserting into the chunks
or files
collections.
更新日志
版本 | 说明 |
---|---|
PECL mongo 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" /> </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']));?>
参见
- 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