微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用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();

?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐