简体   繁体   中英

Downloading Large file from Mongodb using PHP

I have stored more than 250MB zipped file in Mongodb using php. but i am unable to retrieve it back. It just shows me "0" number. 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.

Read the docs, 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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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