PHP 发送的 ZIP 存档已损坏 [英] ZIP Archive sent by PHP is corrupted

查看:35
本文介绍了PHP 发送的 ZIP 存档已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 php ZipArchive 即时创建一个 zip 文件并将其发送回用户.我将压缩文件临时存储在文档根目录上方的文件夹中,然后将其与代码一起发送回

I am using php ZipArchive to create an zip file on-the-fly and send it back to the user. I temporarily store the zipped file in a folder above document root and then send it back with the code

header('Content-type:application/zip');
header('Content-Disposition: inline; filename="'.("file.zip").'"');
header("Content-Transfer-Encoding: binary");
header("Content-Length:".filesize($file));
$fh = fopen($file,'rb');
fpassthru($fh);

在第一次发布之后

$zip->close()

确保文件未打开.我遇到的问题是 - 存储的 zip 文件是一个有效的存档,我可以在 Windows 7、7Zip、WinZIP 等中打开它.但是,当我用上面的代码发送文件时,它以 0xD 0xA 对结束在文件的开头,这足以使其损坏.我无法弄清楚这些字符可能来自哪里.这是 fopen/fpassthru 的已知错误吗?任何帮助将不胜感激.

to ensure that that the file isn't open. The issue I have run into is this - the stored zip file is a valid archive which I can open in Windows 7, 7Zip, WinZIP etc. However, when I send the file down with the code above it ends up with an 0xD 0xA pair at the start of the file which is enough to render it corrupt. I cannot figure out where those characters could be coming from. Is this a known bug with fopen/fpassthru? Any help would be much appreciated.

推荐答案

你的完整脚本是什么样的?

What does your full script look like?

一般来说,您应该删除脚本文件末尾的所有 PHP 结束标记,因为它可能是来自脚本末尾或包含的脚本的输出.

Generally speaking you should remove any closing PHP tags at the end of your script files, as it's probably output coming from the end of your script, or an included script.

这篇关于PHP 发送的 ZIP 存档已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆