【发布时间】:2013-06-18 17:12:08
【问题描述】:
我试图在单击链接时强制下载 PDF,但我不断收到来自 readfile 的标题警告和奇怪的字符。
<a> 标签:
<p>The pdf file is large and can take a minute to download.
You can <a href="/icisherbrooke/downloadAnnuaire.php">click here to download the pdf.</a></p>
download.php 文件:
<?php
$fullpath = 'annuaire_2013-14_optimized3.pdf';
if(file_exists($fullpath)){
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="annuaire_2013-14_optimized3.pdf"');
readfile($fullpath);
exit();
}
?>
当我用echo "test" 替换标题和读取文件时,它可以工作。
我被重定向到download.php,而不是下载提示。以下是 download.php 的部分结果:
Warning: Cannot modify header information - headers already sent by (output started at /home/kanope/vhost_file/info-rapide.com/icisherbrooke/downloadAnnuaire.php:1) in /home/kanope/vhost_file/info-rapide.com/icisherbrooke/downloadAnnuaire.php on line 8
%PDF-1.6 %���� 1561 0 obj <> endobj 1575 0 obj <>/Filter/FlateDecode/ID[<0EC5F18608359D479CC3DB0FF72E23C8><997B135B48FBE541BDE876EE8BECFD9D>]/Index[1561 20]/Info 1560 0 R/Length 95/Prev 216592620/
【问题讨论】:
-
听起来您在第 1 行有一些空格。可能是 BOM。
-
你是对的,它确实是一个 BOM。我将 Notepad++ 中的编码更改为没有 BOM 的 UTF8,并且新文件可以正常工作。如果您将此作为答案发布,我可以接受。谢谢。
标签: php http-headers