2016-05-02 92 views
0

我试着通过头下载zip文件:无效的Zip文件下载(头+ PHP)

$file = '/srv/users/serverpilot/apps/elm/public/dll/files/438de5/file-c117c93c.zip'; 
$filename = 'file-c117c93c.zip'; 
if (file_exists($file)) 
{ 
    $fileName = trim($filename); 
    header('Content-Description: File Transfer'); 
    header('Content-Disposition: attachment; filename='.$fileName); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($file)); 
    ob_clean(); 
    flush(); 
    readfile($file); 
    exit;  
} 

文件将与真实尺寸下载。但是当我打开下载文件时,zip文件无法打开(无效归档错误)

是什么问题?

+0

@Pe dro Lobito同样的错误 – elize

+0

我在我的服务器上尝试了几种解决方案,但我找不到解决方案,我只是将文件的内容看作文本,就像'•ù_ãÃyºŒ'æfè,U³»ƒ'ÔÕdkÈjA',我会继续挖... –

+0

检查我的回答:) –

回答

0

尝试了几个解决方案后,我意识到问题是由Apache发送的Content-Encoding: gzip。我的httpd.conf默认设置为发送这个头,这将导致对文件下载了一个问题:

HTTP/1.1 200 OK 
Date: Mon, 02 May 2016 21:52:08 GMT 
Server: Apache 
x-powered-by: PHP/5.3.3 
content-transfer-encoding: Binary 
Content-Disposition: attachment; filename="peace.zip" 
Vary: Accept-Encoding 
Content-Encoding: gzip 
Connection: close 
Transfer-Encoding: chunked 
Content-Type: application/zip 

在不同的服务器上,返回头不包含Content-Encoding: gzip和你的代码完美的作品。

HTTP/1.1 200 OK 
Date: Mon, 02 May 2016 21:57:43 GMT 
Server: Apache/2.2.15 
x-powered-by: PHP/5.4.45 
Content-Description: File Transfer 
Content-Disposition: attachment; filename="peace.zip" 
content-transfer-encoding: binary 
Expires: 0 
Cache-Control: must-revalidate 
Pragma: public 
Content-Length: 627874 
Connection: close 
Content-Type: application/zip 

解决方案:

我已经奋斗了约1小时与此...
添加以下在你的PHP文件的开头:

apache_setenv('no-gzip', 1); 
ini_set('zlib.output_compression', 0); 

Voilá,强制下载工程...