PHP operation MongoDBGridFS storage file details. Copy the code as follows :? Php initialize gridfs $ connnewMongo (); connect to MongoDB $ db $ conn-photos; select database $ grid $ db-getGridFS (); get gridfs object gridfs has
The code is as follows:
// 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
?>
The http://www.bkjia.com/PHPjc/327718.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327718.htmlTechArticle code is as follows :? Php // initialize gridfs $ conn = new Mongo (); // Connect to MongoDB $ db = $ conn-photos; // select database $ grid = $ db-getGridFS (); // Obtain the gridfs object // gridfs has...