【发布时间】:2011-08-05 23:03:31
【问题描述】:
我在使用 PHP 在 Chrome 中读取 pdf 文件时遇到问题。
以下代码是我在PHP中的做法
$path = "actually file path";
header("Pragma: public");
header("Expires: 0");
header("Content-type: $content_type");
header('Cache-Control: private', FALSE);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Disposition: inline; filename=\"$filename\"");
header('Content-Transfer-Encoding: binary');
header('Content-Length' . filesize($path));
ob_clean();
flush();
readfile($path);
在这里,我将 Content-Disposition 设置为内联。因为如果用户浏览器有内置的 pdf 查看器插件,我想显示 pdf 文件。您可能知道,Chrome 有内置的 pdf 查看器。
问题是我在服务器上有一堆 pdf 文件。 Chrome 只能查看其中的一部分。我不明白为什么其他人不能以同样的方式工作。我已经检查了每个文件的权限。看起来不是权限问题。
有谁知道问题出在哪里?谢谢。
【问题讨论】:
-
改为强制下载(Content-disposition: attachment/Content-type: application/octet-stream),下载/保存好的PDF和坏的pdf,比较保存的和正在播放的服务器。
-
@Marc 我试试。它们都可以下载并且看起来一样。我还将它与服务器中的文件进行了比较。他们是一样的。当我切换回 Content-disposition: inline 时。它只是行不通。 :( 我什至比较了响应头。它们是一样的。
-
如果您尝试直接查看而不是通过脚本会发生什么?
-
@Marc 您无需通过脚本即可直接查看。
-
那么脚本的意义何在?
标签: php pdf google-chrome