Home > Backend Development > PHP Tutorial > PHP操作MongoDB GridFS 存储文件的详解_PHP

PHP操作MongoDB GridFS 存储文件的详解_PHP

WBOY
Release: 2016-06-01 12:06:22
Original
1011 people have browsed it

MongoDB

复制代码 代码如下:
//初始化gridfs
$conn = new Mongo(); //连接MongoDB
$db = $conn->photos; //选择数据库
$grid = $db->getGridFS(); //取得gridfs对象

//gridfs有三种方式存储文件
//第一种直接存储文件
$id = $grid->storeFile("./logo.png");

//第二种存储文件二进制流
$data = get_file_contents("./logo.png");
$id = $grid->storeBytes($data,array("parame"=>'附加参数将随图片一起存入'));

//第三种保存直接表单提交的文件$_FILES
$id = $grid->storeUpload('upfile');
//相当于
$id = $grid->storeFile($_FILES[‘upfile'][‘tmp_name']);

//--------------以上是保存图片--下面开始读取图片----------------

//保存成功后返回$id = md5字符串
$logo = $grid->findOne(array('_id'=>$id)); //以_id为索引取得文件
header('Content-type: image/png'); //输出图片头
echo $logo ->getBytes(); //输出数据流
?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template