2012-04-06 124 views
1

我在Mongodb中使用php存儲了超過250MB的壓縮文件。但我無法找回它。它只顯示我「0」號碼。以下是我的代碼。使用PHP從Mongodb下載大文件

$id = $_GET['id']; 
require 'dbconnection.php'; 


$mongo = DBConnection::instantiate(); 
$gridFS = $mongo->database->getGridFS(); 

$object = $gridFS->findOne(array('_id' => new MongoId($id))); 
header("Content-Transfer-Encoding: binary"); 
header('Content-type: '.$object->file['filetype']); 
header('Expires: 0'); 


header("Content-disposition: attachment; filename=".$object->file['filename']); 
//header('Content-name: '.$object->file['filename']); 
    header('Content-Type:application-x/force-download'); 
echo $object->getBytes(); 

檢查它。這適用於小於100MB的文件。

+1

它最有可能與使用所有可用內存有關。 – 2012-04-06 10:52:04

回答

2

閱讀文檔,http://php.net/manual/en/mongogridfsfile.getbytes.php

警告:這將文件加載到內存中。如果文件比你的記憶大 ,這會造成問題!

那麼你就需要大文件存儲的塊,然後通過循環和回聲塊,我搜羅谷歌和發現這個(3顯示瞭如何訪問基於塊的文件):

http://blog.hardkap.com/index.php/posts/00069/MongoDB---GridFS

+0

對不起,我從上面的鏈接。上傳的254 MB文件創建了26 MB的塊大小。在檢索文件時,它會逐個獲取塊並下載它。我不認爲它會從大塊到內存中獲得整個254MB,然後下載它。 – 2012-04-10 08:00:50