Recent projects need to use MongoDB access files, here is a simple PHP example:
Public Function run () {
Initialize Gridfs
$m = new Mongoclient (); Connection
$db = $m->selectdb ("Excel");
Dump ($m), exit;
$collection = $db->testexcel;
$grid = $db->getgridfs (); Get Gridfs Object
Gridfs has three ways of storing files
The first type of direct storage file
$id = $grid->storefile (dirname (__file__). /emptyaction.class.php ");
echo $id;
Get files
$file = $grid->findone (Array (' _id ' => $id)); Take _id as index to get files
Header (' content-type:image/png '); Output Picture Header
Dump ($file);
}
View all uploaded files on the MongoDB command line:
1.show DBS
List all databases
2.use Excel
Go to a database named Excel
3.db.fs.files.find ()
List all uploaded files
Upload the file through Gridfs, it is passed to fs.files this collection by default. Gridfs has two sets, "Fs.chunks" and "Fs.files", the former is fragmented storage, the latter is normal file storage, suitable for less than 4M of files.