最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Download file size is unknown although file size is present in header - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Sep 17, 2012 at 10:40 m90m90 11.8k15 gold badges67 silver badges119 bronze badges 1
  • 1 Could you check with fiddler or similar that the browser doesn't do a HEAD request first to get the file size? If so, you may need to return the same information when you get that request. – Joachim Isaksson Commented Sep 17, 2012 at 10:44
Add a ment  | 

1 Answer 1

Reset to default 7

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_pression', 0);

Filesize headers are sent and received correctly now in all browsers

发布评论

评论列表(0)

  1. 暂无评论