<?php
//Initialize gridfs
$conn = new Mongo(); //Connect to MongoDB
$db = $conn->photos; //select database
$grid = $db->getGridFS(); // get gridfs object
$grid = $db->getGridFS(‘file’); //Get the file gridfs object
//gridfs has three ways to store files
//The first type of direct storage file
$id = $grid->storeFile(“./logo.png”); $id = $grid->put(‘./logo.png’, array(‘ower’=>’myname’));
//storeFile has the same effect as put, followed by additional parameters, which are saved in the files file
//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 picture’));
//The third way to save the file $_FILES submitted directly by the form
$id = $grid->storeUpload(‘upfile’);
// equivalent to
$id = $grid->storeFile($_FILES[‘upfile’][‘tmp_name’]);
//————–The above is to save the picture–begin to read the picture below—————-
//Return $id = md5 string after saving successfully
$logo = $grid->findOne(array(‘_id’=>$id)); //Use _id as the index to get the file, or directly file name
header(‘Content-type: image/png’); // output image header
echo $logo ->getBytes(); // output data stream
$grid->remove() delete file
$grid->delete() also deletes files, but only the _id of the file can be passed
$grid->drop() clears all data
$grid->find() and findOne() directly search for files and return IDs, find() does not add parameters, and directly returns all file objects
$grid->getFilename() returns the filename
$grid->getSize() returns the file size
?>