PHP Download Script создает нечитаемый ZIP файл на Mac
Для справки, я уже прочитал и попробовал ответы в этих и нескольких других темах:
Создание и обслуживание сжатых файлов с помощью php
Открытие загруженного zip-файла создает файл cpgz?
У меня есть почтовый файл на моем сервере.
When I use Filezilla to move that Zip file from my server to my Mac, I can open it normally.
When I use this PHP code to download the Zip file to my Linux machine, it opens normally.
When I use this PHP code to download the Zip file to my Mac, using Safari or Firefox, I get an error saying "Decompression Failed" or "The structure of the archive is damaged" or I get a .cpgz file - which I believe means that the computer is zipping the file, not unzipping it.
Вот код PHP, который я использую для доставки zip-файла.
$zipname = "myfile.zip";
$zippath = "/path/to/" . $zipname;
if ($downloadzip = fopen ($zippath, "r")) {
$fsize = filesize($zippath);
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$zipname."\"");
header("Content-length: $fsize");
header('Content-Transfer-Encoding: binary');
#header("Cache-control: private"); //use this to open files directly
echo fpassthru($downloadzip); // deliver the zip file
}
fclose ($downloadzip);
Я нашел несколько заголовков, которые работают. Я действительно не знаю и не волнуюсь, почему это работает, я просто счастлив, что это работает ... Я пробовал кучу разных вещей, файлов .htaccess, настроек php.ini / zlib.
Вот ответ http://perishablepress.com/http-headers-file-downloads/
$zipName = 'myfile.zip';
$zipPath = 'mydirectory/' . $zipName;
if (file_exists($zipPath)) {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$zipName."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($zipPath));
ob_end_flush();
@readfile($zipPath);
}