Created
May 23, 2012 12:00
-
-
Save sharpred/2774847 to your computer and use it in GitHub Desktop.
retrieve mongo attachment data, iterate through and retrieve each file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$mongoDB = new Mongo(); | |
$database = $mongoDB->selectDB("BVS"); | |
$collection = $database->createCollection('fs.files'); | |
//to get the attachments metadata back | |
$query = array("metadata.formdata.claimid" => "SUS14052012-001"); | |
// $items is a cursor of mongodata | |
$items = $collection->find($query); | |
//iterate through the collection and retrieve the named file | |
$grid = $database->getGridFS(); | |
foreach ($items as $item => $value) { | |
$fileName =$value["metadata"]["uploaddata"]["filename"]; | |
// md5 represents a unique key for a file so is a good one to use | |
$md5 = $value["md5"]; | |
$file = $grid->findOne(array("md5" => $md5)); | |
$target = './' . $fileName; | |
$file->write($target); | |
}?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment