如何使用PHP在MongoDB上保存文件?

如何在MongoDB上保存文件
我使用 PHP,它似乎没有任何信息..
另外,如果你想给我一些例子来了解如何保存和下载这些文件,我将非常感谢

解决方法

查看本教程: http://learnmongo.com/posts/getting-started-with-mongodb-gridfs/

粘贴一些代码将其包含在答案中:

<?PHP

// Connect to Mongo and set DB and Collection
$mongo = new Mongo();
$db = $mongo->myfiles;

// GridFS
$grid = $db->getGridFS();

// The file's location in the File System
$path = "/tmp/";

$filename = "03-smbd-menu-screen.mp3";

// Note Metadata field & filename field
$storedfile = $grid->storeFile($path . $filename,array("Metadata" => array("filename" => $filename),"filename" => $filename));


// Return newly stored file's Document ID
echo $storedfile;

?>

要恢复文件

<?PHP
// Connect to Mongo and set DB and Collection
$mongo = new Mongo();
$db = $mongo->myfiles;     

// GridFS
$gridFS = $db->getGridFS();     

// Find image to stream
$image = $gridFS->findOne("chunk-screaming.jpg");

// Stream image to browser
header('Content-type: image/jpeg');
echo $image->getBytes();

?>

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...