【发布时间】:2023-03-08 07:09:01
【问题描述】:
我正在尝试创建、下载然后删除一个 gzip 压缩文件。
在服务器上创建的文件是正确的。我可以通过直接链接下载它,并且文件在本地正确解压缩。
如果我使用 header(),文件将不会解压缩并给出错误“Not In Gzip Format”。看来我必须使用 header(),才能 unlink()。
代码的要点是:
# This creates the gz file correctly..
$tar = 'meta-info.tar';
$gz = $tar.'.gz';
$p = new PharData($tar);
$p->compress(Phar::GZ)->buildFromDirectory('../');
# This creates the dialog then removes the file correctly..
header('Content-Type:'.mime_content_type($gz));
header('Content-Length:'.filesize($gz));
header("Content-Disposition:attachment;filename=$gz");
flush();
readfile($gz);
unlink($gz);
# ...but the downloaded file is not correct.
奇怪的是,“zip”文件可以正确下载和解压缩。
我查看了整个 stackoverflow 并尝试了很多方法,但没有任何效果。有什么想法吗?
提前致谢!
【问题讨论】:
-
那么实际发送的是什么 Content-Type 标头?使用您的浏览器开发控制台检查您得到的响应。
-
您看不到什么?如果您下载一个文件,那么您肯定执行了一个请求,并且肯定会显示在开发控制台中。您知道如何打开它并使用其中的“网络”标签吗?
-
AFAICS
application/x-gzip不属于通常支持的官方 mime 类型... -
可能是http服务器激活的zip压缩模块拦截了?不过听起来很奇怪……
-
你看过那些数据了吗?这是网页的开头:
<!DOCTYPE html><html lang="en"><meta charset="ut。整个页面说了什么?您可能只是收到网页形式的未找到错误。