PHP Velho Oeste 2024

MongoGridFS::storeUpload

(PECL mongo >=0.9.0)

MongoGridFS::storeUploadアップロードされたファイルをデータベースに格納する

説明

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

パラメータ

name

アップロードされたファイルの name フィールド。 これは、HTML フォームの file フィールドの name 属性と同じでなければいけません。

metadata

格納するファイルに含めるその他のメタデータフィールド。

注意:

これらのフィールドは、ドライバが自動生成したフィールドも上書きします。詳しい説明は、MongoDB コアドキュメントの » files collection を参照ください。この挙動の現実的な使い道としては、ファイルの chunkSize_id を独自に指定する場合などがあります。

注意:

filename フィールドには、クライアントのファイル名 ($_FILES['foo']['name'] など) が入ります。

返り値

格納したファイルドキュメントの _id を返します。metadata パラメータで _id を明示的に指定していない場合は、自動生成した MongoId となります。

注意:

複数のファイルを同じフィールド名でアップロード した場合は、このメソッドは何も返しません。 しかし、ファイル自体の処理はそのまま行われます。

エラー / 例外

アップロードされたファイルの読み込みに失敗したり、 chunks あるいは files コレクションへの追加に失敗したりした場合に MongoGridFSException をスローします。

変更履歴

バージョン 説明
1.2.5 二番目のパラメータがメタデータの配列に変わりました。これより前のバージョンでは、 二番目のパラメータはオプションの文字列で、ファイル名を上書きするものでした。

例1 MongoGridFS::storeUpload() を使った HTML フォームの例

こんな HTML フォームがあるものとします。

<form method="POST" enctype="multipart/form-data">
    <label for="username">ユーザー名</label>
    <input type="text" name="username" id="username" />

    <label for="pic">プロフィール画像をアップロードしてください</label>
    <input type="file" name="pic" id="pic" />

    <input type="submit" />
</form>

アップロードされたファイルを MongoDB に格納するには、フォームの投稿を処理するスクリプト側でこのようにします。

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

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

参考

add a note add a note

User Contributed Notes 3 notes

up
0
valentin at hilbig dot de
6 years ago
For my PHP 7.0.27 this is incompatible to the "multiple" tag on inputs.

Like this:

<input type="file" name="files[]" multiple>

Please note that you need to use 'files[]', as else only one file is used from the uploaded files.

As documented, you need to give the Index into $_FILES as the first argument to this function.  Like in:

foreach ($_FILES as $k=>$v)
   $db->getGridFS()->storeUpload($k, $v);

However in the case of "multiple" selects, the corresponding entry in $_FILES[$k]["name"] is an array of filenames, not just a single filename.  This is true even for a single file, in that case it is an array with just a single string in it.

In that case the function just silently fails.  No error, no problem shown, it just becomes a NOP.

The workaround is:

- Without JavaScript, fallback to single file upload.
- With JavaScript add "multiple", and do the upload with Ajax file-by-file.
- As then only single files reach PHP, the problem is solved.
up
-1
juris dot zeltins at gmail dot com
5 years ago
For multiple uploads storeUploads should be implemented.

But as we have possibility to use direct storeFile then it makes no sense because custom form was used anyway.
up
-4
kkasid94 at gmail dot com
7 years ago
I am not able to upload an image using this code.
Getting this error

Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::getGridFS() in C:\xampp\htdocs\phpmongodb\new.php:4 Stack trace: #0 {main} thrown in C:\xampp\htdocs\phpmongodb\new.php on line 4
To Top