【问题标题】:Header - Downloaded Gzip Not In Gzip Format, Correct On Server标头 - 下载的 Gzip 不是 Gzip 格式,在服务器上正确
【发布时间】: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。整个页面说了什么?您可能只是收到网页形式的未找到错误。

标签: php download gzip


【解决方案1】:

问题是标头信息已经发送。

使用 ob_end_clean() 修复它(页面结构无法更改)。

ob_end_clean();
header("Content-Disposition:attachment;filename=$gz");
header('Content-Type:application/x-gzip');
header('Content-Length:'.filesize($gz));
flush();
readfile($gz);
unlink($gz);

【讨论】:

  • 在我的情况下,这段代码工作得很好 ob_end_clean(); header("内容类型:应用程序/tar+gzip"); header('Content-Disposition:attachment;filename="'.$filename.'"'); readfile(''.$filename.'');
猜你喜欢
  • 2014-06-24
  • 1970-01-01
  • 1970-01-01
  • 2011-01-13
  • 2012-07-17
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2016-03-08
相关资源
最近更新 更多