rss

Force a file to download

4 May, 2010 Advanced,Page,Single

Image by: vizzzual-dot-com

In building CMS sites for clients, some may require that you offer certain files to be downloaded off their site. A PDF for example, or perhaps wallpapers or other media. Most browsers, correctly, will display these items inline as a new page or tab however for these purposes you need to force the browser to download the item, not display it. Here’s how it’s done.

The Code

Your .htaccess file is the key to getting this done, and here’s the code you need to add:

AddType application/octet-stream .csv
AddType application/octet-stream .xls
AddType application/octet-stream .doc
AddType application/octet-stream .avi
AddType application/octet-stream .mpg
AddType application/octet-stream .mov
AddType application/octet-stream .pdf

You can of course add as many other filetypes to this list as you may require, but you can get the principle from this example.

7 Comments

  1. Thanks. I really appreciate this. I have a website that I put songs I’ve made on it and I’ve been looking for a way to make this work. thanks to you I can. :)

  2. Awesome! Spent all day looking for this! Thanks a lot. I am bookmarking your page now!

  3. F*ckin’ tremendous things here. I’m very glad to look your post. Thanks so much and i’m having a look ahead to contact you. Will you kindly drop me a mail?

  4. Works perfect. Thank you.

  5. thanks for sharing!!! :D this is all what we need to bypass those bloody paid site

  6. Opens up a blank page in chrome…not totally stable. I found this to be the solutions for all browsers:

    ForceType application/octet-stream
    Header set Content-Disposition attachment

  7. Just delete the ** marks (but keep the one inside next to .pdf) and copy/paste into .htaccess

    ****
    ForceType application/octet-stream
    Header set Content-Disposition attachment
    ****

Leave a Reply

About The WordPress Guru

Over the last 4 or 5 years, I’ve been digging into the Wonders of WordPress. From the days when the Classic theme was the shizz until now, WordPress has gone from strength to strength both as the blogging platform of choice for many big players and as a CMS with...

Learn more »