Force Browser to Download MP3's

Joined: 06/14/2007
User offline. Last seen 3 years 7 weeks ago.

Does anyone know how to force the browser to popup the "save as" dialog box when user clicks on a link to a MP3? This is currently what happens on the Geeks&God website.

I have tried adding this to my .htaccess on the server in the directory that contains the MP3 files:

AddType application/octet-stream .mp3

<Files *.mp3>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>

The above directives work for Firefox but not IE7. I have tried each directive alone as well as together in the .htaccess file.

Any help would be appreciated.

Thanks,

Justin

G&G Podcast Host
Rob Feature's picture
Joined: 06/01/2006
User offline. Last seen 1 week 39 min ago.
A browser thing...I think

Hey Justin...
It may be because I'm a dummy, but I'm pretty sure this is just a browser thing. I know in most browsers, if you link to the mp3 directly, it will ask you to download it (as long as the browser doesn't try to play it through a player plugin).

That's all we have here. Granted, I think the Audio module is generating that link, but still, I'm pretty sure it's just a standard link to the original mp3 file.

-Rob Feature
Geeks and God Co-Host
www.bobchristenson.com

-Rob Feature
Geeks and God Co-Host
www.mustardseedmedia.com

G&G Moderator
EdRoss's picture
Joined: 10/05/2006
User offline. Last seen 5 weeks 3 days ago.
browser based settings for mp3 downloads

Not sure about getting the server to force a download (though it would be nice to know that), but you could use my video how to get Firefox to download wmv files and just change the wmv to mp3.

Joined: 06/14/2007
User offline. Last seen 3 years 7 weeks ago.
Flumoxed

I just don't get it. On Geeks & God, when I click on the download MP3 links, the File Download dialog box pops up in both Firefox2 and IE7. When I click on the same type of link on my own site, http://www.hopechapelsterling.... ,(same computer, same browsers) the mp3 loads in the browser using a plugin (i.e. Quicktime or Media Player). Depending which computer I use the behavior changes. Firefox plays the mp3 in the browser at home but downloads to disk at work. IE does just the opposite.

I find it very interesting that it works consistently on Geeks & God but not on my site. Ugh!

Thanks for trying,
-Justin

G&G Podcast Host
Matt Farina's picture
Joined: 06/01/2006
User offline. Last seen 3 weeks 2 days ago.
http header

The reason that it always pops up and asks to save with the audio module is that the audio module sets the http header for the file to be something that forces it to download.

Some of the http header elements that it custom sets are Content-Type, Content-Length, and Content-Disposition with Content-Disposition being set (among other things) as an attachment.

You can read the spec here: http://www.w3.org/Protocols/rf...

This is what makes the pop-up box work.

The reason your fix didn't work is because IE doesn't take the server at it's word, reads the files mime type, and does what it would normally do. Your best bet is to do something like what the audio module does (using something like php, asp, perl, python, etc.) and force the http headers.

Matt Farina
Geeks and God Co-Host
www.mattfarina.com

Matt Farina
Geeks and God Former Co-Host
www.mattfarina.com

Joined: 06/14/2007
User offline. Last seen 3 years 7 weeks ago.
Plan B

Thanks for the response. I was hoping to not go that route but I guess I will have to anyway. I wanted the link to be a direct link to the file and not to a backend script. It just seems less clean to me. Such is life.

Just so I fully understand, is it true then that the links to mp3's on G&G are not direct (even though they appear that way to the browser) and are actually handled by Drupal and the Audio module?

-Justin

G&G Podcast Host
Rob Feature's picture
Joined: 06/01/2006
User offline. Last seen 1 week 39 min ago.
Correct

Hey Justin...
You are correct...All the links for our downloads are handled directly by the Audio module in drupal (except for old episodes...you'll notice a different style flash player on those and they were hard coded links).

I just wasn't aware that they had some php magic behind them....all hail MF for his supreme geekyness....

-Rob Feature
Geeks and God Co-Host
www.bobchristenson.com

-Rob Feature
Geeks and God Co-Host
www.mustardseedmedia.com

G&G Podcast Host
Matt Farina's picture
Joined: 06/01/2006
User offline. Last seen 3 weeks 2 days ago.
Another IE Issue

The audio module does do some php magic. It isn't a simple redirect. It's even more than dealing with the header... it actually allows us to keep stats on the files.

The reason you can't link directly to the mp3s is a function of IE. Where the server tells it to download the file IE decides otherwise. The scripting (PHP, Perl, ASP, etc.) is really a work around due to another fun filled 'feature' in IE.

Matt Farina
Geeks and God Co-Host
www.mattfarina.com

Matt Farina
Geeks and God Former Co-Host
www.mattfarina.com

Joined: 06/15/2007
User offline. Last seen 2 years 5 weeks ago.
Forcing download of MP3 files

Justin,

We accomplished this for our podcast feed by setting the MIME TYPE to application/force-download for MP3 files.

Web Servant of the Most High God

<>< <>< <>< <>< <>< <><

My motto and life's goal:
"Simple obedience to God in the simple things." †

Web Servant of the Most High God

<>< <>< <>< <>< <>< <><

My motto and life's goal:
"Simple obedience to God in the simple things." †

G&G Podcast Host
Matt Farina's picture
Joined: 06/01/2006
User offline. Last seen 3 weeks 2 days ago.
noperz

You might not realize this but in my IE on a PC qucktime just plays your file. I'm not prompted to download. Works fine in my mac but not on my PC.... darn IE.

So, when it your cast going to bring me a new episode?

Matt Farina
Geeks and God Co-Host
www.mattfarina.com

Matt Farina
Geeks and God Former Co-Host
www.mattfarina.com

G&G Moderator
EdRoss's picture
Joined: 10/05/2006
User offline. Last seen 5 weeks 3 days ago.
don't stop users getting at the mp3 url though

Just a quick request....in whatever you end up doing, please don't make it hard for users to get to the actual url of the mp3: I've just had a fight with a church web site trying to get to the mp3 url of their latest sermon. I wanted to share it with others (on Facebook), but they were using redirect urls, so it wasn't immediately obvious where the mp3 was. I had to open their "listen now" page, which embedded the mp3, then use Firefox's "View page info" option, and go to the media tab to see where the mp3 was. I could have gone to view source too, but our net connection in slow at the moment, so that wasn't working for me.

G&G Podcast Host
Matt Farina's picture
Joined: 06/01/2006
User offline. Last seen 3 weeks 2 days ago.
slow down there

You are right that users should have access to a url to the audio file. But, that doesn't mean the url has to be a direct link to the file.

On the Geeks and God site users don't have direct access to the file but they do have access to a link to the file. By passing the mp3 through the site we can do all sorts of good things and still leave a link that lets you get through to the file.

Matt Farina
Geeks and God Co-Host
www.mattfarina.com

Matt Farina
Geeks and God Former Co-Host
www.mattfarina.com

TQ
TQ's picture
Files are External

what about if the files are external links. we have a podcast and we save the files on archive.org and I want to use the download link for users to download the mp3s.

G&G Podcast Host
Matt Farina's picture
Joined: 06/01/2006
User offline. Last seen 3 weeks 2 days ago.
A little hacking

Sadly, files on a different server is not supported right now :-(

What you would have to do is combine a few other modules together with a url style field from CCK being one of them or get into some custom coding.

Matt Farina
Geeks and God Co-Host
www.innovatingtomorrow.net
www.mattfarina.com

Matt Farina
Geeks and God Former Co-Host
www.mattfarina.com

James Woods
James Woods's picture
Help!

Hey guys,

Discovered this thread whilst I was googling help for this very issue - loving the site!

I'm trying to achieve the same thing with my website - left clicking a download link forces a download dialog of an mp3. However, I'm relatively uneducated as far as php is concerned.

Is anyone able to provide a bit more of a step-by-step explanation of how I do this??? I would be extreeeemely grateful!

Yours in Christ,

James.

Simplest solution

Sometimes the simplest solution is to just put it in a .zip and call it a day.

Noname
Noname's picture
I'd rather right-click than

I'd rather right-click than extract a .zip.

Joined: 12/20/2008
User offline. Last seen 26 weeks 1 day ago.
Maybe this will answer some

Maybe this will answer some of your questions? http://geeksandgod.com/forums/...

endredy
endredy's picture
this might help

You can use this little php script, in this way: ".../download.php?examplefile.mp3"

download.php:
<?

// force download mp3
$filename = (isset($_REQUEST['file']) ? $_REQUEST['file'] : '');
$filesize = @filesize($filename);
if ($filename == '' || $filesize == 0) exit(0);

header("Content-Description: {$filename}");
header("Content-length: {$filesize}");
header("Content-Disposition: attatchment; filename={$filename}");
readfile($filename);
?>

endredy
endredy's picture
more comfortable

If you add this .htaccess to the prevoius solution, then files can be downloaded in this way: .../examplefile.mp3

benefit: link is cleaner (the trick is invisible for the user), apache can log files

.htaccess

RewriteEngine On
RewriteRule (.*.mp3)$ download.php?file=$1

endredy
endredy's picture
again

tags in .htaccess were eliminated in prev post, sorry :)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule  (.*.mp3)$                   download.php?file=$1
</IfModule>