【问题标题】:PHP download gives me empty pdfPHP下载给了我空的pdf
【发布时间】:2014-09-26 13:28:46
【问题描述】:
$file = "http://www.abax.se/content/download/137/862/file/example.pdf";
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
flush();
readfile($file);

这是我正在运行的代码,我没有想法,有什么解决方案吗?

【问题讨论】:

  • 同样的事情,空下载的pdf...
  • 这段代码看起来很奇怪。为什么要下载文件才能上传?为什么不直接发送“301 Moved Permanently”响应,将用户直接发送到文件?该文件是否与此代码位于同一服务器上?如果是这样,那么您不应该通过 HTTP;您应该直接从服务器读取和输出文件。

标签: php pdf download


【解决方案1】:

删除header('Content-Length: ' . filesize($file));。

这就是破坏你的输出的原因。

filesize() 需要一个本地文件,而不是 URL。

工作代码:

<?php
$file = "http://www.abax.se/content/download/137/862/file/example.pdf";
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
flush();
readfile($file);
?>

【讨论】:

  • "加载 PDF 文档失败"
  • 将工作代码添加到答案中。这在 php5.4 上运行良好。
猜你喜欢
  • 2021-10-31
  • 2016-08-07
  • 2020-08-07
  • 2015-04-22
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多