Detailed explanation of PHP operation of MongoDB GridFS storage files_PHP tutorial

WBOY
Release: 2016-07-21 15:05:08
Original
773 people have browsed it

Copy code The code is as follows:

//Initialize gridfs
$conn = new Mongo(); //Connect to MongoDB
$db = $conn->photos; //Select the database
$grid = $db->getGridFS(); //Get the gridfs object

//gridfs has three ways to store files
//The first method is to store files directly
$id = $grid->storeFile("./logo.png");

//The second storage file binary stream
$data = get_file_contents("./logo.png");
$id = $grid->storeBytes($data,array("parame" =>'Additional parameters will be stored with the image'));

//The third way to save files submitted by direct forms $_FILES
$id = $grid->storeUpload('upfile');
//Equivalent to
$id = $grid- >storeFile($_FILES['upfile']['tmp_name']);

//-------------The above is the saved image--below to start reading the image-------------

//Return $id = md5 string after successful saving
$logo = $grid->findOne(array('_id'=>$id)); //Get the file using _id as the index
header('Content-type: image/png'); //Output image header
echo $logo ->getBytes(); //Output data stream
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327718.htmlTechArticleCopy the code as follows: ?php //Initialize gridfs $conn = new Mongo(); //Connect to MongoDB $ db = $conn-photos; //Select database $grid = $db-getGridFS(); //Get gridfs object //gridfs has...
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