Abhishek Sharma
Abhishek Sharma

Reputation: 3280

Memory limit avoiding In MongoGridFSFile::getBytes PHP

Suppose my server has 4GB of ram and i uploaded a file having size 5GB. How can i download that file using gridfs. Following site states that http://www.php.net/manual/en/mongogridfsfile.getbytes.php If your file is bigger than memory than its a problem but doesn't tells a solution for that.

Can anyone have any solution for this. i use this demo code to access a file.

<?php
// Connect to Mongo and set DB and Collection
$mongo = new Mongo();
$db = $mongo->myfiles;

// GridFS
$gridFS = $db->getGridFS();

// Find image to stream
$file = $gridFS->findOne("win.tar");

// Stream image to browser
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"win.tar\"");
echo $file->getBytes();
?>

Upvotes: 2

Views: 684

Answers (2)

bjori
bjori

Reputation: 2104

As of version 1.3.0 of the PHP Driver you can access the GridFS Files as a PHP stream, using $MongoGridFSFile->getResource().

Using that method you can iteratively read the data and print it out, avoiding the memory limitation on your server.

Upvotes: 3

Vladimir Muzhilov
Vladimir Muzhilov

Reputation: 786

  • just split a source file by chunk and save meta information about each of these chunks in mongodb, each of your chunk will be ordinary file in gridfs
  • after that, you have a meta layer with meta data about the source file
  • also you must solved problems reverse downloaded file from gridfs and compound source file from chunks
  • size of this chunk you may select based on your network speed and width limitation, this chunks and chunk in gridfs is different

Upvotes: 1

Related Questions