Using Header as download link in PHP

后端 未结 2 867
猫巷女王i
猫巷女王i 2021-02-10 22:09

So I am having this problem wit my download link. Basically my previous way of creating download link is to just have a form button with method=\'link\' and the action to be the

相关标签:
2条回答
  • 2021-02-10 22:15

    Something like this works fine.

    header("Pragma: public", true);
    header("Expires: 0"); // set expiration time
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Disposition: attachment; filename=".basename($file));
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($file));
    die(file_get_contents($file));
    
    0 讨论(0)
  • 2021-02-10 22:18

    If I understand you correctly the script is working as expected, i.e if a browser recognizes mime type:

    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    

    Then it SHOWS it in browser (whether or not it displays correctly is something else)

    Since you want to FORCE it to download INSTEAD OF showing in browser use the mime type:

    'application/octet-stream'
    

    This should work in all browsers and force the download instead of display in browser.

    0 讨论(0)
提交回复
热议问题