[英]php header issue when downloading zip
我正在嘗試使用以下代碼使php下載.zip文件:
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
$ file是正確的。
這個腳本有兩個問題:
如果我使用header('Content-Length: ' . filesize($file));
0字節文件下載已損壞。 如果我對此行發表評論,那么在ubuntu上下載看起來不錯。
如果我使用Mac OS下載此文件,則在打開.zip文件后,它將解壓縮.cpgz文件(因為我看到的是zip損壞時的內容)
我已經嘗試過各種標頭來實現此目的,但問題始終出在該標頭上。 我的問題是:如何在所有操作系統上都可以使用此功能?
我將其用於使用一個通用PHP腳本下載任何類型的文件的目的:
$base = "/path/to/downloadable/files";
$file = "myArchive.zip"; // or anything else
if(file_exists($base.$file)){
header('Content-Description: Download');
header('Content-Type: '.mime_content_type($base.$file));
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Length: ' . filesize($base.$file));
echo file_get_contents($base.$file);
}
這對幾乎所有類型的文件(包括.zip)都適用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.