<?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('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(); //输出数据流?>
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!