0

I am generating a zip file using zip archive and sending it to the browser for the user to download.

$archive_file_name = "/var/www/html/administrator/1396413991.zip";
header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=test.zip"); 
header("Expires: on, 01 Jan 1970 00:00:00 GMT"); 
header("Pragma: no-cache"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
readfile("$archive_file_name");
exit;

My download stops at 11Mb and i am unable to download files more than 11mb, What iam doing wrong or incorrect in this.

Any suggestion would be appreciated. Thanks.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
opensource-developer
  • 2,826
  • 4
  • 38
  • 88
  • 1
    Possible duplicate : http://stackoverflow.com/questions/2946791/php-readfile-and-large-downloads –  Apr 07 '14 at 10:52
  • 1
    Since that looks like it's probably web-accessible, why not simply point at/redirect to the zip file and let the webserver serve it? Serving files with php is at best slower. – AD7six Apr 07 '14 at 10:54
  • Due to the implementation specifics i need to allow the user to download rather the redirecting the user – opensource-developer Apr 07 '14 at 11:05
  • I meant that i have to implement it in such a way that there should not be any redirects – opensource-developer Apr 07 '14 at 11:51
  • That's exactly what you said the first time, but it is illogical to have a requirement and rule out the most obvious solution without a specific reason. E.g. _point at the zip file_ instead of the php file. If instead you _did_ redirect to the zip file - what problem would that cause? Any browser/http client would just follow the redirect and download the zip file. Anyway - sounds like you're set on using a (relatively) complex solution to an incredibly simple problem. – AD7six Apr 07 '14 at 12:17

2 Answers2

1

I would advice in using fpassthru instead, it's been made specifically for this situation.

Note: 

Passthru didn't work for me for files greater than about 5Mb. Just adding "ob_end_clean()", all works fine now, including > 50Mb files.

$ToProtectedFile=$pathUnder.$filename
$handle = @fopen($ToProtectedFile, "rb");

@header("Cache-Control: no-cache, must-revalidate");
@header("Pragma: no-cache"); //keeps ie happy
@header("Content-Disposition: attachment; filename= ".$NomFichier);
@header("Content-type: application/octet-stream");
@header("Content-Length: ".$SizeOfFile);
@header('Content-Transfer-Encoding: binary');

ob_end_clean();//required here or large files will not work
@fpassthru($handle);//works fine now
opensource-developer
  • 2,826
  • 4
  • 38
  • 88
Blizz
  • 8,082
  • 2
  • 33
  • 53
  • Thanks Blizz for you comment, it is specified in the document it is used for binary files, can i use it for zip files? – opensource-developer Apr 07 '14 at 11:13
  • Hi i tried with fpassthru but facing the same issue – opensource-developer Apr 07 '14 at 11:18
  • Hmm that's weird, afaik fpassthru doesn't use all that much memory as it's made to relay files. Did you check your error logs to see what the exact issue is, there should be some indication in them? – Blizz Apr 07 '14 at 11:19
  • I checked the comment on the fpassthru page and it said fpassthru did not work for files more than 5 MB, they have also given a fix there. That did the trick for me, Thanks. – opensource-developer Apr 07 '14 at 11:23
  • The ob_end_clean comment only applies if you're actually using output buffering. If you're about to pass a big file to a visitor, it's best that you turn that off first indeed :) – Blizz Apr 07 '14 at 11:27
  • Well i am not sure wether to use output buffering or not. because without ob_end_clean the code is not working, could you please elaborate what would be best subscribed – opensource-developer Apr 07 '14 at 11:32
  • In your code fragment it's not turned on, but the fact that it works when you stop the buffering suggests that it is enabled. If it works, it is the/a solution for your issue ;) – Blizz Apr 07 '14 at 11:37
0

May be you can try and change your php.ini settings to

memory_limit = 128M
post_max_size = 300M
Abhinav
  • 8,028
  • 12
  • 48
  • 89