1, http://cn2.php.net/manual/en/class.mongogridfs.php
classFilecontrollerextendssycontroller{/** @author YJ * @description to use the following two functions, you must first initialize * date:14-9-3*/ Public functioninit () {Header("Content-type:text/html;charset=utf-8"); //Connect MONGO and Initialize GFS//database naming file $conn=New\mongoclient (); $db=$conn-picdb; //Get Gridfs Object $prefix= ' pic '; $collection=$db->getgridfs ($prefix); return $collection; } /** @author YJ * @description will pass all binaries on to MongoDB and return the file index ID value * Dedicated to form submission resources * DATE:14-9-2 * time:11:21 */ Public functionUpload$filename,$collection) { $id=$collection->storeupload ($filename);//gets the _id string in the ID object of the previous step $id=Strval($id); return $id; } /** @author YJ * @description Delete the corresponding resource based on the ID value * date:14-9-3*/ Public functionDelete$id,$collection) {//Convert ID string to ID object $collection->delete (New\mongoid ($id)); } /** * @author: YJ * Use resource ID and resource type to retrieve and display resources from MongoDB*/ Public functionget () {Header("Content-type:text/html;charset=utf-8"); $conn=New\mongoclient (); $db=$conn-picdb;//Get Gridfs Object $prefix= ' pic '; $collection=$db->getgridfs ($prefix); //get ID and type $id= I (' get.id '); $type= I (' Get.type '); //Return Data $object=$collection->findone (Array(' _id ' =New\mongoid ($id))); Header(' Content-type: '.$type); Echo $object-getBytes (); }
Note that the ' _id ' stored in MongoDB is an object ID, also known as an objects ID. And the id we put into MySQL is a string ID. The conversion method is as follows:
$id Strval ($id); // gets the string ID in the object ID
new \mongoid ($id) // convert string ID to MONGO object ID
2. If you want to use MongoDB in other controllers
//initializing the MongoDB database$collection= R (' File/init ');//use a form to upload a file, $key is the name of the input label in the form$id= R (' File/upload ',Array($key,$collection));//according to the string ID $avatar _id to delete MONGO inside the corresponding. files. chunks file$collection->delete (New\mongoid ($avatar _id));//gets the resource directly from the MONGO based on the string ID and displays it according to the type$avatar _id} "/>
Public Library-mongodb-notes