如何使用PHP在MongoDB上保存文件


how to save files on MongoDB using PHP?

如何在MongoDB上保存文件
我正在使用PHP,它似乎没有任何信息
此外,如果你能给我一些例子来了解如何保存和下载这些肉,我将非常感谢

查看本教程:http://learnmongo.com/posts/getting-started-with-mongodb-gridfs/

粘贴一些代码以将其包含在答案中:

<?php
// Connect to Mongo and set DB and Collection
$mongo = new Mongo();
$db = $mongo->myfiles;
// GridFS
$grid = $db->getGridFS();
// The file's location in the File System
$path = "/tmp/";
$filename = "03-smbd-menu-screen.mp3";
// Note metadata field & filename field
$storedfile = $grid->storeFile($path . $filename,
             array("metadata" => array("filename" => $filename),
             "filename" => $filename));

// Return newly stored file's Document ID
echo $storedfile;
?>

要取回文件:

<?php
// Connect to Mongo and set DB and Collection
$mongo = new Mongo();
$db = $mongo->myfiles;     
// GridFS
$gridFS = $db->getGridFS();     
// Find image to stream
$image = $gridFS->findOne("chunk-screaming.jpg");
// Stream image to browser
header('Content-type: image/jpeg');
echo $image->getBytes();
?>