【发布时间】:2020-07-24 10:43:39
【问题描述】:
我想强制浏览器使用 php.ini 中的readfile 从服务器下载 PDF 文件。
这是我的代码:
$file = '/DIR/file.pdf';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
在该浏览器显示一些未知字符而不是下载 PDF 文件之后。我尝试了不同的浏览器,但都没有工作。 如何强制浏览器下载文件?
输出是这样的:
请注意,如果我在浏览器中输入文件目录,文件会正确下载。
【问题讨论】:
-
我觉得内容头不对,你试过这个:
header("Content-type:application/pdf");? -
使用
Content-Type: application/pdf -
@KIKOSoftware 我试过了,但没用
-
@MarkusZeller 我试过了,但没用
-
header('Content-Encoding: identity')和header("Content-type:application/pdf")