我有一个包含信息的php页面,以及文件的链接,例如pdf文件.文件类型可以是任何内容,因为它们可以由用户上载.
我想知道如何强制下载任何类型的文件,而不强制下载链接到从该网站链接的其他页面.我知道可以使用标题执行此操作,但我不想打破我网站的其余部分.
所有到其他页面的链接都是通过Javascript完成的,实际链接是#,所以也许这样可以吗?
我应该设置
header('Content-Disposition: attachment;)
Run Code Online (Sandbox Code Playgroud)
对于整个页面?
Gum*_*mbo 12
您需要为特定资源发送以下两个标头字段:
Content-Type: application/octet-stream
Content-Disposition: attachment
Run Code Online (Sandbox Code Playgroud)
该Content-Disposition可以另外有一个filename参数.
您可以使用发送文件的PHP脚本来执行此操作:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment');
readfile($fileToSend);
exit;
Run Code Online (Sandbox Code Playgroud)
文件名通过URL传递给该脚本.或者您使用某些Web服务器功能(如mod_rewrite)强制类型:
RewriteEngine on
RewriteRule ^download/ - [L,T=application/octet-stream]
Run Code Online (Sandbox Code Playgroud)
略有不同的风格,准备去:)
$file = 'folder/' . $name;
if (! file) {
die('file not found'); //Or do something
} else {
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($file);
}
Run Code Online (Sandbox Code Playgroud)