简体   繁体   中英

Force file download in php not working

I have this script to force file download in php but its not working. the file still opens on the page. Help me figure out what is wrong.

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

I think you are testing this in firefox or google chrome. Also you are trying to download pdf or txt file I hope. In google chrome Adobe acrobot and in firefox adobe reader plugins are preinstalled.

So there is nothing to do with php code to download. Your code is correct. You can test it by disabling acrobot in google chrome. for doing so visit chrome://plugins/ in your chrome. then disable Chrome PDF Viewer . Then browse the download link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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