使用PHP从Mongodb下载大文件


Downloading Large file from Mongodb using PHP

我使用php在Mongodb中存储了超过250MB的压缩文件。但我无法取回它。它只显示给我"0"号。下面是我的代码。

$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的文件。

阅读文档,http://php.net/manual/en/mongogridfsfile.getbytes.php

警告:这将把文件加载到内存中。如果文件更大比你的记忆力差,这会造成问题!

所以你需要将大文件存储在块中,然后循环并回显块,我搜索了谷歌,发现了这个(3。显示如何访问基于区块的文件):

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