doujiang1993
doujiang1993
2017-01-04 10:39
浏览 78
已采纳

通过PHP下载文件gzip

I am downloading a PDF file that is located on a server (file can be opened) via PHP script:

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;

The downloaded file is gzipped. Smaller in size than original. Cannot be opened until the .gz extension is added at the end and file extracted.

Server API CGI/FastCGI

PHP version 7.1.0

Lighttpd 1.4.35

Request headers

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

Response headers

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

The Content-Length header is correct (the same as on file system).

If I put in full URL pointing to PDF file, the file is downloaded and opened without any problems.

Why or when is the target file silently gzipped after/before it is downloaded?!

Tested on Chrome, Firefox, Safari, Opera

图片转代码服务由CSDN问答提供 功能建议

我正在通过PHP脚本下载位于服务器上的PDF文件(可以打开文件):

  ini_set(“zlib.output_compression”,“Off”);  //只是为了确定
 
 $ quoted = sprintf('“%s”',addcslashes(basename($ fileName),'“\\')); 
 $ size = filesize($ fileName); / 此阶段的/ size是正确的
 
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(); \  nflush(); 
readfile($ fileName); 
 
ini_set(“zlib.output_compression”,“On”); 
exit; 
   
 
 

下载的文件 大小比原始小。在最后添加.gz扩展名并提取文件之前无法打开。

服务器API CGI / FastCGI

PHP版本7.1.0

Lighttpd 1.4.35

请求标题 \ ñ

 <代码>接受:text / html的,applicati  on / 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 
授权:基本************ 
Cache-Control:max-age = 0 
Connection:keep-alive 
Host:dev。*********。lv \  nUpgrade-Insecure-Requests:1 
User-Agent:Mozilla / 5.0(Windows NT 6.1;  WOW64)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 55.0.2883.87 Safari / 537.36 
   
 
 

响应标题 \ n

  Cache-Control:必须重新验证,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”
内容 - 长度:831807 
内容 - 传输 - 编码:二进制
内容 - 类型:application / octet-stream 
日期:2017年1月4日星期三10:22:19 GMT \  nExpires:0 
Last-Modified:Wed,04 Jan 2017 10:22:19 GMT 
Pragma:public 
Server:FS 
X-Powered-By:PHP / 7.1.0 
   \  n 
 

Content-Length标头正确(与文件系统相同)。

如果我输入指向PDF文件的完整URL,则下载并打开文件 没有任何问题。

目标文件在下载之后/之前是否或何时默默地进行gzip压缩?!

在Chrome,Firefox,Safari上测试过 ,Opera

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douque8855
    douque8855 2017-01-06 08:56
    已采纳

    For some reason

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

    did not do its job and the variable remained "On". Even though this parameter is of PHP_INI_ALL class and entry can be set anywhere.

    Setting this variable to Off in php.ini file solved the problem but also rised a new question regarding runtime configuration variables.

    calling phpinfo(); before readfile() allowed me to track down the bug.

    点赞 评论

相关推荐