Created
February 16, 2012 16:42
-
-
Save kevinholler/1846342 to your computer and use it in GitHub Desktop.
MongoDB GridFS test
This file contains hidden or 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 | |
$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