用 PHP 強制檔案下載的方法
以下程式碼可用於強制在 PHP 中下載檔案。
<?php header('Content-type: text/javascript'); header('Content-Disposition: attachment; filename="file.js"'); readfile(file that is downloaded.js'); //This can be printed for verification purpose ?>
注意 - 這需要在顯示任何輸出之前完成,否則該檔案也會有其他操作產生的輸出(可能不相關)。
另一種可能使用的方法是使用 .htaccess 解決方案。在此方法中,伺服器上的所有檔案都可以被強制下載,並且可以新增到 .htaccess 檔案中。這在下面已經進行了演示 -
AddType application/octet-stream csv header('Content-Type: application/csv'); header('Content-Disposition: attachment; filename=name of csv file'); header('Pragma: no-cache'); readfile("path-to-csv-file");
廣告