php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63337 MongoGridFS::storeUpload() stores multiple files with same $metadata
Submitted: 2012-10-23 08:35 UTC Modified: 2013-01-09 06:17 UTC
From: kgo_yoi at hotmail dot com Assigned: bjori (profile)
Status: Closed Package: mongo (PECL)
PHP Version: Irrelevant OS: Ubuntu
Private report: No CVE-ID: None
 [2012-10-23 08:35 UTC] kgo_yoi at hotmail dot com
Description:
------------
---
From manual page: http://www.php.net/mongogridfs.storeupload
---

PHP Version: PHP 5.3.10-1ubuntu3.4 with Suhosin-Patch (cli) (built: Sep 12 2012 18:59:41)
MongoDB Support	enabled, Version 1.2.12

In HTML, there are 2 file input fields,

<form method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="file" name="file2">
    <input type="submit" />
</form>

If I store the 2 files into MongoDB with same meta data, the filename of the second file will be overridden by the filename of the first file.

For example, I selected file 'php-logo.png' with the first file input field and file 'photo.jpg' with the second file input field.

Test script:
---------------
$metadata = array('foo' => 'bar');
$files = array_keys($_FILES);	
foreach ($files as $file) { 
    $mongo->selectDB('foo')->getGridFS()->storeUpload($file, $metadata);
}

$result = $mongo->selectDB('foo')->getGridFS()->find();			
foreach ($result as $id => $file) {
    echo "{$id}: ", $file->getFilename(), ' (', $file->getSize(), ' Bytes), PHP_EOL;
}

Expected result:
----------------
50865626902e96792600001a: php-logo.png (6237 Bytes)
50865626902e96792600001d: photo.jpg (3941543 Bytes)

Actual result:
--------------
50865626902e96792600001a: php-logo.png (6237 Bytes)
50865626902e96792600001d: php-logo.png (3941543 Bytes)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-23 10:13 UTC] kgo_yoi at hotmail dot com
Like http://www.php.net/manual/en/class.mongogridfs.php Inter-Language Compatibility section said, "some drivers expect that all metadata associated with a file be in a "metadata" field. If you're going to be using other languages, it's a good idea to wrap info you might want them to see in a "metadata" field.". If I put all metadata in a "metadata" field of an associated array and pass the array to storeUpload(), the second filename will remain unchanged.

$mongo->selectDB('foo')->getGridFS()->storeUpload($file, array('metadata' => $metadata));
 [2013-01-09 06:17 UTC] bjori@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: bjori
 [2013-01-09 06:17 UTC] bjori@php.net
This is expected behaviour.

The $metadata argument will be populated with the filename (just like we populate 
the _id field for normal inserts).

On the next execution, the filename field in the metadata array is already 
populated with "file1" - and will therefore be used as the filename for file2.

You could unset the $metadta["filename"] after each ->uploadFile() call, or reset 
the array to "workaround this issue".
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Apr 10 23:01:24 2021 UTC