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

fna*_*ira 3 php mongodb

如何在MongoDB上保存文件?
我正在使用PHP,它似乎没有任何信息..
另外,如果你想给我一些例子,知道如何保存和下载那些fles我将非常感谢

Eve*_*man 13

查看本教程: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;

?>
Run Code Online (Sandbox Code Playgroud)

要恢复文件:

<?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();

?>
Run Code Online (Sandbox Code Playgroud)