加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 22:09:29 所属栏目:PHP教程 来源:网络整理
导读:如何在MongoDB上保存文件? 我使用 PHP,它似乎没有任何信息.. 另外,如果你想给我一些例子来了解如何保存和下载这些文件,我将非常感谢 解决方法 查看本教程: http://learnmongo.com/posts/getting-started-with-mongodb-gridfs/ 粘贴一些代码将其包含在答案
如何在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();

?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读