php中的强制文件下载不起作用


Force file download in php not working

我有这个脚本来强制在php中下载文件,但它不起作用。文件仍然在页面上打开。帮我找出问题所在。

<?php
$orderid=sql_prep($_GET['oid']);
if(isset($_POST['file'])){
    $file = $_POST['file'];
    header("Content-Description: File Transfer"); 
    header("Content-Transfer-Encoding: Binary");
    header("Content-Type: application/octet-stream"); 
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    readfile('attachments/'.$orderid.'/user/'.$file);
}
?>
<form action="" method="POST" name="dwnld" >
  <input name="file" value="<?php echo $file['filekey']; ?>" type="hidden">
  <button type="submit"> <?php echo $file['filename']; ?> </button>
</form>

我认为您正在firefox或googlechrome中测试这一点。另外,我希望你正在尝试下载pdf或txt文件。在谷歌chrome中,Adobe acrobot和firefox中预装了Adobe阅读器插件。

因此,下载php代码与此无关。您的代码是正确的。你可以通过在谷歌chrome中禁用acrobot来测试它。访问chrome://plugins/在你的铬。然后禁用Chrome PDF Viewer。然后浏览下载链接。