Reputation: 11822
I am doing the follwoing on a web page: A click on an element sends a set of data (attached to the element) to my server which will then generate a custom zip-file:
$.post(urlprefix + 'makeZip.php', params, function(data){
window.location = urlprefix + 'getZip.php?file=' + data; //get file
}).error(handleAJAXError);
makeZip.php
works just fine and returns the name of the (temporary) zip file that the client should then download. As I want to keep my server clean I route the file through another script called getZip.php
which does the following:
/* RETURN REQUESTED FILE AND DELETE FROM SERVER*/
$filename = $_GET['file'];
/* TRANSFER FILE CONTENTS */
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="customDownload.zip"');
header('Content-Length: '.filesize($filename));
readfile($filename);
/* REMOVE FILE FROM SERVER */
unlink($filename);
All browsers will download the file successfully, yet I am facing one problem: the files can get rather big (up to 200MB) so I thought it would be nice to have an estimate of how long the download's going to take. That's why I am sending the Content-Length
header (the specified filesize is correct). Yet, all browsers I tested this is are telling me the filesize is unknown (which might lead to the user skipping the download).
Is this some kind of problem with my header information? Is it a client-side problem? Should I use another approach to getting the client to download the file?
Upvotes: 2
Views: 4472
Reputation: 14678
If the client shows "unknown filesize" in the dialog when downloading a file provided via readfile, you'd better check if mod_deflate
, mod_gzip
, mod_something-that-shrinks-http
is installed on your server, and put an exception for given download.
More info here
EDIT by m90:
In my particular case (running Apache) I turned shrinking off by using:
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
Filesize headers are sent and received correctly now in all browsers
Upvotes: 7