繁体   English   中英

强制在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或Google Chrome浏览器对此进行测试。 我也希望您正在尝试下载pdf或txt文件。 在google chrome中, Adobe acrobot和firefox adobe reader中已预装插件。

因此,与php代码下载无关。 您的代码是正确的。 您可以通过在Google chrome中禁用acrobot对其进行测试。 为此,请在您的Chrome浏览器中访问chrome:// plugins /。 然后禁用Chrome PDF Viewer 然后浏览下载链接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM