【发布时间】:2017-01-04 10:39:58
【问题描述】:
我正在通过 PHP 脚本下载位于服务器上的 PDF 文件(文件可以打开):
ini_set("zlib.output_compression", "Off"); // just to be sure
$quoted = sprintf('"%s"', addcslashes(basename($fileName), '"\\'));
$size = filesize($fileName); // size at this stage is correct
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
ob_clean();
flush();
readfile($fileName);
ini_set("zlib.output_compression", "On");
exit;
下载的文件被 gzip 压缩。尺寸比原版小。在末尾添加 .gz 扩展名并提取文件之前无法打开。
服务器 API CGI/FastCGI
PHP 版本 7.1.0
Lighttpd 1.4.35
请求标头
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Authorization:Basic ************
Cache-Control:max-age=0
Connection:keep-alive
Host:dev.*********.lv
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
响应标头
Cache-Control:must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Description:File Transfer
Content-Disposition:attachment; filename="INV_AUC15-3_U1_DDDDD-IIII_03112016.pdf"
Content-Length:831807
Content-Transfer-Encoding:binary
Content-Type:application/octet-stream
Date:Wed, 04 Jan 2017 10:22:19 GMT
Expires:0
Last-Modified:Wed, 04 Jan 2017 10:22:19 GMT
Pragma:public
Server:FS
X-Powered-By:PHP/7.1.0
Content-Length 标头正确(与文件系统上的相同)。
如果我输入指向 PDF 文件的完整 URL,文件将被下载并打开,没有任何问题。
为什么或何时在下载目标文件之后/之前对其进行静默 gzip 压缩?!
在 Chrome、Firefox、Safari、Opera 上测试
【问题讨论】: