Copy codeThe Code is as follows: <? Php
// Initialize gridfs
$ Conn = new Mongo (); // connect to MongoDB
$ Db = $ conn-> photos; // select a database
$ Grid = $ db-> getGridFS (); // get the gridfs object
// There are three methods for storing files in gridfs
// The first method for directly storing files
$ Id = $ grid-> storeFile ("./logo.png ");
// Binary stream of the second storage file
$ Data = get_file_contents ("./logo.png ");
$ Id = $ grid-> storeBytes ($ data, array ("parame" => 'additional parameters will be saved together with the image '));
// The third method is to save the FILES submitted directly in the form $ _ FILES
$ Id = $ grid-> storeUpload ('upfile ');
// Equivalent
$ Id = $ grid-> storeFile ($ _ FILES ['upfile'] ['tmp _ name']);
// -------------- Save the image above -- start reading the image below ----------------
// Returns the $ id = md5 string after it is saved successfully.
$ Logo = $ grid-> findOne (array ('_ id' => $ id); // retrieves a file using _ id as an index
Header ('content-type: image/png '); // output the image header
Echo $ logo-> getBytes (); // output data stream
?>