簡體   English   中英

PHP 強制下載不適用於 ZIP 文件

[英]PHP Force download not working for ZIP file

我正在嘗試使用headers下載 zip 文件。 但它返回一些特殊字符。

我試過下面的代碼。

$filepath='http://localhost/mb/inc/tmp/liberty/xml_invoice.zip'
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="test.zip');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
ob_clean();
flush();
readfile($filepath);
exit();

當我運行此代碼時,我得到的輸出類似於一些特殊字符。如下所示

�Q�s�+k�X�ߡB�R�EE����m��@;Ok����A���R�2vZ�j�F�Mo� C�

如果您想提供 ZIP 文件,那么您還需要使用正確的 MIME 內容類型!

$filename = "xml_invoice.zip"
$file = "C:\wamp64\www\mb\inc\tmp\liberty\{$filename}";
header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename={$filename}");
header("Content-length: " . filesize($file));
header("Pragma: no-cache"); 
header("Expires: 0"); 
readfile($file);

試試這個:給你的 zip 文件的路徑。

$filepath = "/path/to/some_file.zip";
 $file_name = basename($filepath);
 header("Content-Type: application/zip");
 header("Content-Disposition: attachment; filename=$file_name");
 header("Content-Length: " . filesize($filepath));
 readfile($filepath);
 exit;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM