2012-02-29 75 views

回答

12

檢查本教程出: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(); 

?> 
+0

太好了。非常感謝你=) – fnaquira 2012-03-02 14:01:07

+0

偉大的...你可以請告訴我使用getBytes .... – 2013-02-25 13:57:26

相關問題