可以在 PHP 中创建 zip 存档,但无法正确下载它2

Can create zip archive in PHP, but cannot download it correctly2

我想对此post“link”发表评论,但我的声誉低于tan 50,所以我不得不再次提出几乎相同的问题。

我从解压缩(从我的网站下载后)收到以下错误消息。

End-of-central-directory signature not found. Either this file is not a zipfile, or it constitutes one disk of a multi-part archive. In the latter case the central directory and zipfile comment will be found on the last disk(s) of this archive. unzip: cannot find zipfile directory in one of foo.zip or foo.zip.zip, and cannot find foo.zip.ZIP, period.

在我的 lampp 服务器上(测试设置)(位于 /opt/lampp/myprojects/tmp/foo.zip)解压完美(没有错误消息)。

我正在使用此代码:

$filename = 'foo.zip';
$path = '/opt/lampp/myprojects/tmp/foo.zip';
if(!file_exists($path))
{
    die('Error: file not found');
}
else {
    $handle = fopen($path, "r");
    header('Content-Description: File Transfer');
    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename='.$filename);
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($path));

    flush();
    readfile($path);
    fclose($handle);



  

谁能看到我的错误并帮助我解决这个问题?我试图修复它大约 5 小时,但我无法解决问题。

<?php
$filename = 'foo.zip';
$path = '/tmp/foo.zip';
if(!file_exists($path)) {
    die('Error: file not found');
}

header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
header('Content-Length: ' . filesize($path));

readfile($path);

使用以下方式将您的 ZIP 文件发送到 user/browser。没有理由先打开它。

ob_end_clean();
$FileBytes = filesize($temp_file);

header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="WhateverYourFileNameIs.zip"');
header('Content-Length: ' . $FileBytes);

readfile($temp_file);