Skip to content

Instantly share code, notes, and snippets.

@kevinholler
Created February 16, 2012 16:42
Show Gist options
  • Save kevinholler/1846342 to your computer and use it in GitHub Desktop.
Save kevinholler/1846342 to your computer and use it in GitHub Desktop.
MongoDB GridFS test
<?php
$uploaddir = sys_get_temp_dir()."/";
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
// Connect to Mongo and set DB and Collection
$mongo = new Mongo("mongodb://user:pass@host:port/db");
$db = $mongo->selectDB('db');
// GridFS
$grid = $db->getGridFS();
// The file's location in the File System
$path = $uploaddir;
$filename = $_FILES['userfile']['name'];
// Note metadata field & filename field
$storedfile = $grid->storeFile($path . $filename,
array("metadata" => array("filename" => $filename),
"filename" => $filename));
$gridFS = $db->getGridFS();
// Find image to stream
$image = $gridFS->findOne("$filename");
// Stream image to browser
header('Content-type: image/jpeg');
echo $image->getBytes();
} else {
echo "Upload failed";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment