MongoGridFS
PHP Manual

MongoGridFS::storeUpload

(PECL mongo >=0.9.0)

MongoGridFS::storeUploadStores an uploaded file in the database

Descrição

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

Parâmetros

name

The name of the uploaded file 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.

Nota:

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.

Nota:

The filename index will be populated with the filename used.

Valor Retornado

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

Changelog

Versão Descrição
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.

Exemplos

Exemplo #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']));
?>

Veja Também


MongoGridFS
PHP Manual