2016-05-02 18 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

私のサーバー上でいくつかのソリューションを試しましたが、解決策を見つけることができませんでした。ファイルの内容が '?掘り続ける... –

+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á、force download works ...

関連する問題