How to force a download with a “Save As” prompt using a .htaccess file and/or Apache

14 Jan 2010 Ferado

This is an oldie but a goody. In web development there comes a time when you need to force a download when someone clicks on a link, without telling them to “right click save as…” In the past this was always a problem. Now thanks to Google, you can type in keywords and find a nice, lovely blog post like this one and figure it out. And of course you’re not going to tell anyone because you want to look like a Rock Star. So you’re going to say “ain’t nothing but a thang.”

And before you do that, here’s how you’re going to save the day and prompt a “Save As” to the user so he/she can download his/her file:

[cc lang=”ini”]

# Put all your extensions after the word stream
AddType application/octet-stream .doc .mov .avi .pdf .xls .mp4 .xlsx .docm

[/cc]

Put the preceding code in your Apache config or your .htaccess file. And be sure to enable .htaccess if you don’t already have it enabled.

Again, just do something like this. And by the way, you can even create “fake” extensions if you want. Trust me, sometimes you want to make things up.

[cc lang=”ini”]

# Example
# AddType application/octet-stream

AddType application/octet-stream .xml

[/cc]

Note, I’ve had bad luck with IE6 using .txt as an extension. But don’t worry because IE6 is evil and anyone using it is infected with a virus.

That’s it for now. Stay tuned to my blog. I’m in the process of writing some PHP to better handle .htaccess files.

5 comments on “How to force a download with a “Save As” prompt using a .htaccess file and/or Apache

  1. Pingback:Anonymous

  2. Bradley

    “But don’t worry because IE6 is evil and anyone using it is infected with a virus.”

    AHAHAHAHAHAHA! that is so true!