ZIP 文件下载 php readfile() 错误

ZIP file download php readfile() error

我知道这个问题在这个论坛上已经发布了很多次,但是请相信我,我已经尝试了所有可能的建议解决方案,但它对我没有用。

我正在尝试使用 zip 下载多个文件,虽然 zip 文件下载成功,但它已损坏,我在记事本中打开后遇到错误:

Warning: readfile(E:\Downloads/IMG-20140831-WA0000.zip) [function.readfile]: failed to open stream: No such file or directory in...

我尝试了论坛中提到的所有可能的解决方案,例如 header-check、网络服务器用户对我正在创建 ZIP 文件的文件夹具有写入权限、error-checking 下载之前等,但是没有成功。

执行错误检查后,我遇到了类似

的情况

Error creating ZIP file : IMG-20140831-WA0000.zip

我的代码片段:

function zipFilesDownload($file_names, $archive_file_name, $file_path) {
    $zip = new ZipArchive;
    if ($zip->open($archive_file_name, ZipArchive::CREATE) !== TRUE) {
        exit("cannot open <$archive_file_name>\n");
    }
    foreach($file_names as $files) {
        $zip->addFile($file_path . $files, $files);
    }
    if ($zip->close() === false) {
        exit("Error creating ZIP file : " . $archive_file_name);
    }
    if (file_exists($archive_file_name)) {
        header("Content-Description: File Transfer");
        header("Content-type: application/zip"); 
        header("Content-Disposition: attachment; filename=" . $archive_file_name . "");
        header("Pragma: no-cache");
        header("Expires: 0");
        readfile("E:\Downloads/" . $archive_file_name);
        ob_clean();
        flush();
        exit;
    } else {
        exit("Could not find Zip file to download");
    }
}
$fileNames = array(
    'D:\xampp\htdocs\BE\Multimedia/' . $fullName,
    'D:\xampp\htdocs\BE\Decrypt/' . $decrypt_file
);
$zip_file_name = $actualName . '.zip';
$file_path = dirname("E:\Downloads") . '/';
zipFilesDownload($fileNames, $zip_file_name, $file_path);

请提出一些解决方案。

问题看起来是这一行:

$file_path = dirname("E:\Downloads") . '/';

目录名函数“Returns parent directory's path”。这意味着 $file_path 将是 E:\.

在您的函数中,您在 $zip->addFile() 方法中使用了 $file_path,以引用应添加到 ZIP 存档中的文件。

换句话说,如果您有一组文件,例如:

$files = array(
    'file1.txt',
    'file2.txt',
    'file3.txt',
);

那么将添加到存档中的文件将是:

E:\file1.txt
E:\file2.txt
E:\file3.txt

您可能想要添加这些文件:

E:\Downloads\file1.txt
E:\Downloads\file2.txt
E:\Downloads\file3.txt

据我所知,要修复您的代码,您只需要 而不是 使用 dirname(),就像这样:

$file_path = "E:\Downloads\";