微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我想为这个帖子“link”发表评论,但我的声誉低于 50,所以我不得不再次问几乎同样的问题。

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

未找到中央目录结尾签名。要么这个文件不是 一个 zip 文件,或者它构成一个多部分存档的磁盘。在里面 后一种情况,中央目录和 zipfile 注释将在 此存档的最后一个磁盘。解压缩:找不到 zipfile 目录 在 foo.zip 或 foo.zip.zip 之一中,找不到 foo.zip.ZIP,句点。

在我的 lampp 服务器 (testsetup) (at /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 文件发送到用户/浏览器。没有理由先打开它。

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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。