Downloading Files my download stops at 11MB using PHP - php

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.

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

May be you can try and change your php.ini settings to
memory_limit = 128M
post_max_size = 300M

Related

php download excel file

I write php code downloading excel file, when I test it on my localhost everything fine, the file can be open, however when I go to my server and I test the code, the file is no more readable. Here is my code
$file = basename($path);
if(file_exists($path)){
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Content-Type: application/vnd.ms-excel");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($path));
readfile($path);
exit;
}
for the envir , I use PHP Version 7.2.19 in local and 7.0.33 in my server, and I try all the content-type that i found but no one work .
ob_clean();
put that code before all header declaration.

php file_get_contents / header server difference

I am accessing spreadsheets which are not located inside in the web directory and serving them to the browser. I am using the following code to do this:
$filePath = WEBSITE_ROOT_PATH.'/../where_the_spreadsheets_are/file_name.xls';
if(file_exists($filePath)){
$contents = file_get_contents($filePath);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=file_name.xls");
header("Content-Transfer-Encoding: binary");
echo $contents;
}
This code works correctly on 20+ separate servers, but not on one. On the one that fails Excel says that the spreadsheet is corrupt.
The server that fails has a mirror server, so the same file path and file name exists, and on this server the spreadsheet is ok.
It's a Win server 2007 running WAMP. All the rest are also Win Servers running WAMP.
Does anyone know the reason for this?

zip file download using php in ie8

I was able to zip a folder and download the zip file using PHP code in "chrome" and "FF" browsers. But when I try that in IE8 it shows the downloaded zip file as a file of unknown type. I will have to open it with rar application specifically but I am looking to make this zip file download directly as .zip file in IE8 as it is happening in the other browsers. The following are the headers I used for it:
header("Pragma: public");
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($archive_file_name));
header("Content-Disposition: attachment; filename=$archive_file_name");
readfile("$archive_file_name");
unlink($archive_file_name);
Can someone please let me know where I need to correct the code? Thanks
header("Content-Disposition: attachment; filename=$archive_file_name");
Make sure $archive_file_name is just a name not a whole path, also it needs to be url encoded and should probably have a ".zip" extension at the end.
header("Content-type: application/zip");
Why is the t in "type" lower cased?
You are overwriting some headers, some are needed just for IE, some for other browsers, IE is more picky.
Look at this link:
http://www.boutell.com/newfaq/creating/forcedownload.html

Large zip downloads failing only in IE

I have a little script which sets the header to force download of a zip file with:
if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}
header("Pragma: public");
header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$mp3.'"');
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".#filesize($file));
#readfile($file) OR die("<html>...</html>");
exit;
Everything works fine in Chrome and Firefox but fails in IE 9. I have tried many different ways of setting those headers after googling around but nothing helps. Then I tried a different smaller zip file and it worked! Then I tried a bigger zip file and again fail. Would this be some setting on the server or the php.ini or something? Why does it only affect IE?
When it fails in IE it looks like it downloads.. says completed. But the file is 0 bytes.
Thanks for any help!
If the file is large enough you may have issues related to memory, execution time, etc.
Instead of trying to read the file, you could use the x-sendfile header:
header('x-sendfile: '.$file);
Check out this article about it: http://www.jasny.net/articles/how-i-php-x-sendfile/

IE download incomplete even though it claims success

I'm serving a zipped up file for my clients to download and it works in every OS/Browser except for IE.
The zipped file is 550 MB. When I use IE 8 the file downloads between 50 - 70 MBs and then claims that the download is complete even though the original file is 550 MB.
I'm wondering if I missing some required headers or something. Here is the php code I'm using to serve the file.
<?php
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$dnfilename");
header("Content-Length: ".filesize($ZIPFILE));
header("Pragma: no-cache");
header("Expires: 0");
readfile("$ZIPFILE");
?>
I don't know if this helps, but the following works for me on IE8 - 9, Chrome and Firefox using a 1.5GB zip file.
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$dnfilename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($ZIPFILE));
readfile($ZIPFILE);
Have a look at this article: HTTP Headers for ZIP File Downloads
I have had the same problem in IE7 and IE8. IE9 and other browsers worked just fine. If you change your Content-Type header, it works.
Change
header("Content-type: application/zip");
to
header("Content-type: application/octet-stream");

Categories