简体   繁体   English

使用PHP从Mongodb下载大文件

[英]Downloading Large file from Mongodb using PHP

I have stored more than 250MB zipped file in Mongodb using php.我使用 php 在 Mongodb 中存储了超过 250MB 的压缩文件。 but i am unable to retrieve it back.但我无法取回它。 It just shows me "0" number.它只显示“0”号。 Below is my code.下面是我的代码。

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

Check it.核实。 This works fine for file less than 100MB.这适用于小于 100MB 的文件。

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

Warning: this will load the file into memory.警告:这会将文件加载到内存中。 If the file is bigger than your memory, this will cause problems!如果文件比你的内存大,这会导致问题!

So you would need to store large files in chunks and then loop through and echo the chunks, I scoured google and found this (3. Shows how to access files based on chunks):所以你需要将大文件存储在块中,然后循环并回显块,我搜索了谷歌并找到了这个(3.展示了如何基于块访问文件):

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

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

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