Home >php教程 >PHP开发 >Detailed explanation of how PHP MongoDB GridFS stores files

Detailed explanation of how PHP MongoDB GridFS stores files

高洛峰
高洛峰Original
2016-12-23 09:48:321838browse

<?php
//初始化gridfs
$conn = new Mongo(); //连接MongoDB
$db = $conn->photos; //选择数据库
$grid = $db->getGridFS(); //取得gridfs对象

gridfs has three ways to store files

The first one stores files directly

$id = $grid->storeFile("./logo.png");

The second one stores file binary stream

$data = http://www.bkjia.com/PHPjc/get_file_contents("./logo.png");
$id = $grid->storeBytes($data,array("parame"=> 'Additional parameters will be stored together with the picture'));

The third way to save files submitted directly through the form $_FILES

$id = $grid->storeUpload(&#39;upfile&#39;);//相当于
$id = $grid->storeFile($_FILES[‘upfile&#39;][‘tmp_name&#39;]);
//--------------以上是保存图片--下面开始读取图片----------------//
保存成功后返回$id = md5字符串$logo = $grid->findOne(array(&#39;_id&#39;=>$id));
 //以_id为索引取得文件
header(&#39;Content-type: image/png&#39;);
//输出图片头
echo $logo ->getBytes();
//输出数据流?>

Thanks for reading, I hope it can help everyone, thank you for your support of this site !

For more detailed explanations of PHP MongoDB GridFS file storage methods and related articles, please pay attention to the PHP Chinese website!

Statement:
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