Using Header as download link in PHP

后端 未结 2 865
猫巷女王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));
    

提交回复
热议问题