Copy Code code as follows:
<?php
Initialize Gridfs
$conn = new Mongo (); Connect MongoDB
$db = $conn->photos; Select Database
$grid = $db->getgridfs (); Get Gridfs Object
Gridfs has three ways of storing files
The first type of direct storage file
$id = $grid->storefile ("./logo.png");
Second storage file binary stream
$data = Get_file_contents ("./logo.png");
$id = $grid->storebytes ($data, Array ("Parame" => ' additional parameters will be deposited with the picture '));
The third type of file to save direct form submissions $_files
$id = $grid->storeupload (' upfile ');
Equivalent
$id = $grid->storefile ($_files[' upfile '] [' tmp_name ']);
--------------above is to save the picture--start reading the picture below----------------
Returns $ID = MD5 string after successful save
$logo = $grid->findone (Array (' _id ' => $id)); Take _id as index to get files
Header (' content-type:image/png '); Output Picture Header
echo $logo->getbytes (); Output data stream
?>