Skip to content

Instantly share code, notes, and snippets.

@imjacobclark
Created May 17, 2012 21:13
Show Gist options
  • Save imjacobclark/2721622 to your computer and use it in GitHub Desktop.
Save imjacobclark/2721622 to your computer and use it in GitHub Desktop.
GridFS MongoDB
<?php
$m = new Mongo();
$db = $m->database;
$collection = $db->images;
$cursor = $collection->find();
foreach ($cursor as $obj) {
$unique_id = $obj['test'];
echo "<img src='newupload.php?unique_id=".$unique_id."'>";
}
?>
{
"_id": ObjectId("4fb43613ee3c47c31e000002"),
"files_id": ObjectId("4fb43613ee3c47c31e000001"),
"n": 0,
"data": "<Mongo Binary Data>"
}
{
"_id": ObjectId("4fb43613ee3c47c31e000001"),
"chunkSize": 262144,
"filename": "1335567298_4.png",
"length": 11077,
"md5": "3c180a7e5c06210065a83db581ac24b1",
"test": "test3",
"uploadDate": ISODate("2012-05-16T23: 19: 47.473Z"),
"username": "joe"
}
<?php
try {
$posted_id = $_GET['unique_id'];
$conn = new Mongo;
$db = $conn->database;
$grid = $db->getGridFS();
$file = $grid->findOne(array('test' => $posted_id));
echo $file->getBytes();
exit;
$conn->close();
}catch(MongoConnectionException $e){
die('Error connecting to MongoDB server');
}catch(MongoException $e){
die('Error: ' . $e->getMessage());
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment