如何实现Content-Disposition:附件?

我试图让我的网站上的MP3是通过左键单击下载,而不是必须右键单击并保存,所以为了做到这一点,我必须设置内容处置:附件。 这是我的第一个网站,所以我是新的如何实际做到这一点,但是我在我的HTML标记做这个,或者我设置此与我的托pipe网站?

这里是我的标记看起来像一个例子。

<div class="download"> <a href="MP3/Morgan Page, Sultan & Ned Shepard, and BT feat. Angela McCluskey.mp3" <img src="img/dlicon.png"/></a> </div> 

MP3列表示例:

 <a href="download.php?file=testing.mp3">Download MP3</a> <a href="download.php?file=testing2.mp3">Download MP3</a> 

download.php:

 <?php $file = $_GET['file']; header('Content-type: audio/mpeg'); header('Content-Disposition: attachment; filename="'.$file.'"'); ?> 

正如其他人所说,你不这样做在HTML中,dynamic的解决scheme(例如,使用PHP)是矫枉过正。

在你的情况下,我会在Web服务器configuration中configurationContent-Disposition标头。 对于Apache,您可以根据位置设置标题,或者使用匹配某些文件名的.htaccess文件。

在PHP中也有一个特殊的function:

bool http_send_content_disposition ( string $filename [, bool $inline = false ] )

请参阅PHP手册 : http : //de2.php.net/manual/en/function.http-send-content-disposition.php

Interesting Posts