MongoDB - GridFS
Intro from the MongoDB website:GridFS is a storage specification for large objects in MongoDB. It works by splitting large object into small chunks, usually 256k in size. Each chunk is stored as a separate document in a chunks collection. Metadata about the file, including the filename, content type, and any optional information needed by the developer, is stored as a document in a files collection.
So for any given file stored using GridFS, there will exist one
document in files collection and one or more documents in the chunks
collection.
GridFS links from the MongoDB website:
http://www.mongodb.org/display/DOCS/GridFS
http://www.mongodb.org/display/DOCS/GridFS+Specification
PHP MongoGridGS Class
http://php.net/manual/en/class.mongogridfs.php
Example:
http://www.lightcubesolutions.com/blog/?p=209
I tested this out in PHP and following are some sample codes that may help.
1. PHP code to store uploaded file into the MongoDB GridFS.
<?php
if (isset($_POST["PHPSESSID"])) {
session_id($_POST["PHPSESSID"]);
}
session_start();
$conn = new Mongo("127.0.0.1:27017"); // Connect
$db = $conn->uniform_server; // Select DB
$db->authenticate("user","password"); // Authenticate to MongoDB
$grid = $db->getGridFS(); // Initialize GridFS
$name = $_FILES['Filedata']['name']; // Get Uploaded file name
$type = $_FILES['Filedata']['type']; // Try to get file extension
$id = $grid->storeUpload('Filedata',$name); // Store uploaded file to GridFS
/* Mime types did not work properly on my setup, hence the following logic */
$ext = substr($name,-3);
switch($ext) {
case "jpg": $type = "image/jpeg"; break;
case "gif": $type = "image/gif"; break;
case "png": $type = "image/png"; break;
case "txt": $type = "text/plain"; break;
case "pdf": $type = "application/pdf"; break;
case "zip": $type = "application/x-zip"; break;
}
/* Add additional metadata related to the file if required */
$files = $db->fs->files;
$files->update(array("filename" => $name), array('$set' => array("contentType" => $type, "aliases" => null, "metadata" => null)));
$conn->close(); // Close connection
exit(0);
?>
if (isset($_POST["PHPSESSID"])) {
session_id($_POST["PHPSESSID"]);
}
session_start();
$conn = new Mongo("127.0.0.1:27017"); // Connect
$db = $conn->uniform_server; // Select DB
$db->authenticate("user","password"); // Authenticate to MongoDB
$grid = $db->getGridFS(); // Initialize GridFS
$name = $_FILES['Filedata']['name']; // Get Uploaded file name
$type = $_FILES['Filedata']['type']; // Try to get file extension
$id = $grid->storeUpload('Filedata',$name); // Store uploaded file to GridFS
/* Mime types did not work properly on my setup, hence the following logic */
$ext = substr($name,-3);
switch($ext) {
case "jpg": $type = "image/jpeg"; break;
case "gif": $type = "image/gif"; break;
case "png": $type = "image/png"; break;
case "txt": $type = "text/plain"; break;
case "pdf": $type = "application/pdf"; break;
case "zip": $type = "application/x-zip"; break;
}
/* Add additional metadata related to the file if required */
$files = $db->fs->files;
$files->update(array("filename" => $name), array('$set' => array("contentType" => $type, "aliases" => null, "metadata" => null)));
$conn->close(); // Close connection
exit(0);
?>
2. PHP code to list all the files in the GridFS (in a particular database)
<?php
$conn = new Mongo("127.0.0.1:27017"); // Connect
$db = $conn->uniform_server; // Select DB
$db->authenticate("user","password"); // Authenticate to MongoDB
$grid = $db->getGridFS(); // Initialize GridFS
$cursor = $grid->find();
foreach ($cursor as $obj) { // iterate through the results
echo 'Filename: '.$obj->getFilename().' Size: '.$obj->getSize().'<br/>';
}
$conn->close(); // Disconnect from Server
exit(0);
?>
$conn = new Mongo("127.0.0.1:27017"); // Connect
$db = $conn->uniform_server; // Select DB
$db->authenticate("user","password"); // Authenticate to MongoDB
$grid = $db->getGridFS(); // Initialize GridFS
$cursor = $grid->find();
foreach ($cursor as $obj) { // iterate through the results
echo 'Filename: '.$obj->getFilename().' Size: '.$obj->getSize().'<br/>';
}
$conn->close(); // Disconnect from Server
exit(0);
?>
3. PHP code to download files stored in GridFS
<?php
$conn = new Mongo("127.0.0.1:27017"); // Connect
$db = $conn->uniform_server; // Select DB
$db->authenticate("user","password"); // Authenticate to MongoDB
$grid = $db->getGridFS(); // Initialize GridFS
$ask = $_REQUEST['file']; // Get filename requested
$file = $grid->findOne(array('filename' => $ask));
$files = $db->fs->files;
$file1 = $files->findOne(array('filename' => $ask));
$id = $file->file['_id'];
if ( (substr($ask,-3) == 'zip') || (substr($ask,-3) == 'pdf') ) {
/* Any file types you want to be downloaded can be listed in this */
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$ask);
header('Content-Transfer-Encoding: binary');
$cursor = $db->fs->chunks->find(array("files_id" => $id))->sort(array("n" => 1));
foreach($cursor as $chunk) {
echo $chunk['data']->bin;
}
}
else {
header('Content-Type: '.$file1["contentType"]);
echo $file->getBytes();
}
$conn->close(); // Disconnect from Server
exit(0);
?>
$conn = new Mongo("127.0.0.1:27017"); // Connect
$db = $conn->uniform_server; // Select DB
$db->authenticate("user","password"); // Authenticate to MongoDB
$grid = $db->getGridFS(); // Initialize GridFS
$ask = $_REQUEST['file']; // Get filename requested
$file = $grid->findOne(array('filename' => $ask));
$files = $db->fs->files;
$file1 = $files->findOne(array('filename' => $ask));
$id = $file->file['_id'];
if ( (substr($ask,-3) == 'zip') || (substr($ask,-3) == 'pdf') ) {
/* Any file types you want to be downloaded can be listed in this */
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$ask);
header('Content-Transfer-Encoding: binary');
$cursor = $db->fs->chunks->find(array("files_id" => $id))->sort(array("n" => 1));
foreach($cursor as $chunk) {
echo $chunk['data']->bin;
}
}
else {
header('Content-Type: '.$file1["contentType"]);
echo $file->getBytes();
}
$conn->close(); // Disconnect from Server
exit(0);
?>
4. PHP code to delete a file from GridFS
<?php
$conn = new Mongo("127.0.0.1:27017"); // Connect
$db = $conn->uniform_server; // Select DB
$db->authenticate("user","password"); // Authenticate to MongoDB
$grid = $db->getGridFS(); // Initialize GridFS
$filename = $_REQUEST["file"]; // Get requested filename
$file = $grid->findOne($filename); // Find file in GridFS
$id = $file->file['_id']; // Get the files ID
$grid->delete($id); // Delete the file
$conn->close(); // Disconnect from Server
exit(0);
?>
$conn = new Mongo("127.0.0.1:27017"); // Connect
$db = $conn->uniform_server; // Select DB
$db->authenticate("user","password"); // Authenticate to MongoDB
$grid = $db->getGridFS(); // Initialize GridFS
$filename = $_REQUEST["file"]; // Get requested filename
$file = $grid->findOne($filename); // Find file in GridFS
$id = $file->file['_id']; // Get the files ID
$grid->delete($id); // Delete the file
$conn->close(); // Disconnect from Server
exit(0);
?>
Tags: mongodb,programming - Visits: 10822 - No Comments