<? PHP // Initialize gridfs $ Conn = New Mongo (); // Connect to MongoDB $ DB = $ Conn -> Photos; // Select Database $ Grid = $ DB -> Getgridfs (); // You can retrieve gridfs objects. // There are three ways to store files. // The first method is to store files directly. $ ID = $ Grid -> Storefile ("./logo.png" ); // Second, stores the binary stream of a file. $ Data = Get_file_contents ("./logo.png" ); $ ID = $ Grid -> Storebytes ( $ Data , Array ("Parame" => 'Additional parameters will be stored along with the image ')); // Third, 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 -------------- // After the image is saved successfully, $ id = MD5 string is returned. $ Logo =$ Grid -> Findone ( Array ('_ Id' => $ ID )); // Use _ id as the index to retrieve files Header ('Content-type: image/PNG '); // Output Image Header Echo $ Logo -> Getbytes (); // Output Data Stream ?>