2017-01-04 119 views
1

后下载我下载的是通过PHP脚本位于服务器上(文件可以打开)PDF文件:gzip压缩的文件通过PHP

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标头是正确的(与文件系统上的相同)。

如果我把完整的URL指向PDF文件,该文件被下载并打开时没有任何问题。

为什么或什么时候目标文件在被下载之后/之前默默地gzip?!

测试在Chrome,火狐,Safari,歌剧

回答

0

出于某种原因

ini_set("zlib.output_compression", "Off"); 

没有做好自己的工作和变量仍然是 “开”。尽管这个参数是PHP_INI_ALL类,并且条目可以在任何地方设置。

在php.ini文件中将此变量设置为Off解决了问题,但也提出了有关运行时配置变量的新问题。

调用phpinfo(); readfile()之前允许我追踪错误。