繁体   English   中英

使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM