PHP头文件没有提示下载


PHP header not prompting download

我卡住了…出于某种原因,无论我怎么尝试,我都无法让浏览器提示我下载文件。我已经把下面的代码降到最低限度,试图解决它。我错过了什么?当我运行这个命令时,没有提示。当我查看页面时,它看起来像是在读取文件,它只是一堆不可读的字符。

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

尝试在内容处理头中使用原始文件名。你可以尝试这样写代码:

<?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.