PHP标题没有提示下载

时间:2014-08-13 04:25:34

标签: php header download

我被困了...... 出于某种原因,无论我尝试什么,我都无法让浏览器提示我下载文件。我已将以下代码降至最低,以尝试对其进行故障排除。我错过了什么?当我运行它时,没有给出提示。当我查看页面时,它看起来像是在读取文件,它只是一堆不可读的字符。

<?php
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename='downloaded.pdf'");
readfile("docs/contract.pdf");
?>

1 个答案:

答案 0 :(得分:1)

尝试使用Content Disposition标头中的原始文件名。您可以尝试这样的代码:

<?php
header("Content-type: application/pdf");
header("Content-Transfer-Encoding: Binary");  
header("Content-Disposition: attachment; filename=contract.pdf");
readfile("docs/contract.pdf");
?>

此外,Ensure no output before sending headers. Sending/modifying HTTP headers must be invoked before any output is made. Otherwise the call fails.