問題描述
需要在 PHP 中添加什么代碼才能在訪問鏈接時自動讓瀏覽器將文件下載到本地機(jī)器?
What code do you need to add in PHP to automatically have the browser download a file to the local machine when a link is visited?
我特別想到了類似于下載站點(diǎn)的功能,一旦您單擊軟件名稱,就會提示用戶將文件保存到磁盤?
I am specifically thinking of functionality similar to that of download sites that prompt the user to save a file to disk once you click on the name of the software?
推薦答案
在輸出文件前發(fā)送以下標(biāo)題:
Send the following headers before outputting the file:
header("Content-Disposition: attachment; filename="" . basename($File) . """);
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($File));
header("Connection: close");
@grom:關(guān)于application/octet-stream"MIME 類型很有趣.我不知道,一直只使用應(yīng)用程序/強(qiáng)制下載":)
@grom: Interesting about the 'application/octet-stream' MIME type. I wasn't aware of that, have always just used 'application/force-download' :)
這篇關(guān)于如何在 PHP 中自動開始下載?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!