19

I am trying to make it so that mp3's on my site are downloaded by left clicking instead of having to right click and save as, So in order to do that, I have to set the Content-Disposition: attachment. This is my first website so I am new to how to actually do this, but do I do this in my html markup or do I set this somehow with my hosting site?

Here is an example of what my markup looks like.

<div class="download">
<a href="MP3/Morgan Page, Sultan & Ned Shepard, and BT feat. Angela McCluskey.mp3" 
<img src="img/dlicon.png"/></a>
</div>
Zul
  • 3,627
  • 3
  • 21
  • 35
Jean Hules
  • 415
  • 3
  • 6
  • 12
  • 3
    That's a server-side operation and cannot be done via HTML code on the client. You'll need a script on the server which can act as the download server, and it's that script which will issue the Content-Disposition headers. – Marc B Jan 16 '12 at 05:09
  • Could you provide some more information? Which web server you are using? – Ramesh Soni Jan 16 '12 at 05:12
  • I am using HostGator.com – Jean Hules Jan 16 '12 at 05:22

3 Answers3

25

Example of MP3 Lists:

<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.'"');

?>
Zul
  • 3,627
  • 3
  • 21
  • 35
  • 4
    The code above will fail with double quotes and non-ASCII characters in filenames (see RFC 6266). – Julian Reschke Jan 16 '12 at 08:34
  • @JulianReschke yes, OP should replace filename (see question, file name is: Morgan Page, Sultan & Ned Shepard, and BT feat. Angela McCluskey.mp3). I'll update my answer. – Zul Jan 16 '12 at 08:37
  • 6
    Note: unless you actually need access controls on the mp3s, it's better just to configure the server to serve those mp3s with the content-disposition header set. Using PHP as a file server slows things down a bit, particularly with large files. – Sam Dufel Oct 05 '13 at 00:28
  • 8
    This code have nasty file inclusion exploit. Please always validate $_GET['file'] variable ! – Alex Kolarski Jan 22 '15 at 14:29
  • @SamDufel - How would I go about doing what you suggest? Thanks. –  Jun 19 '15 at 14:20
4

As others have said, you don't do that in HTML, and a dynamic solution (e.g., using PHP) is overkill.

In your case, I'd configure the Content-Disposition header in the web server config. For Apache, you could set the header based on location, or have a .htaccess file that matches certain filenames.

Community
  • 1
  • 1
user3468501
  • 101
  • 2
  • 2
    A dynamic solution for this happens all the time in app development. Imagine for example dealing having to authenticate before permitting privileged content. The authentication check and file stream often need to come from the same request. – JoshuaDavid Sep 02 '14 at 16:38
1

There's a special function for that in PHP, too:

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

See PHP Manual here: http://de2.php.net/manual/en/function.http-send-content-disposition.php

gruentee
  • 323
  • 2
  • 12
  • 6
    Note: `http_send_content_disposition` is *not* available natively, but via a PECL extension: `PECL pecl_http >= 0.10.0` – zamnuts Jul 12 '14 at 09:37