Code Snippet

Home » Code Snippets » HTAccess » Force Files to Download (Not Open in Browser)

Force Files to Download (Not Open in Browser)

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

Subscribe to The Thread

  1. make sure to put this poop in .htaccess

  2. Wow, I’ve been trying to figure out how to do this for a very, very (very) long time! Is there any way you can make it specific, for example, if I click “link 1″, I can view the file in browser, but if I click “link 2″, the “AddType” function is applied, and I am forced to download?

    Thanks in advance, and sweet snippet!

    • It’s not an ideal solution to your problem, but you could have a php wrapper for download links that simply takes an argument that is the filename and then reads the file (assuming local files) and before outputting the file to the user you send headers with the filetype. see http://www.php.net/manual/en/function.header.php for more information about php and headers.

  3. This is a great one. I had forgotten about it before but I’m glad I found it again!

    Thanks

  4. What abouton a windows server? I can’t get the .htaccess for to work – is this only for apache users?

  5. Hi!, that was awesome, i just found this tutor on google,
    thanks!, and now its possible to not use header function.

  6. I m using apache2triad and in this there is no .htaccess named file so now what todo?

    • now stop trying to be a webmaster until you know what you’re doing

  7. I like it short ;)
    AddType application/octet-stream .doc .mov .avi .pdf .xls .mp4

  8. Wow, beautiful trick! Was trying to figure this thing out forever, thank you!

  9. Awesome!
    Thank youy so much for that trick.

  10. great post /…… :)

  11. Federico González Brizzio

    Hi,

    Some people use:

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

    Do you know wich’s better?

    Thanks!

  12. I have used AddType application/octet-stream .png and it forces the download, the only issue is that it opens a new empty tab that it closes after it finishes the donwload, but if I hit the cancel button then the new empty tab stays open. Is there anyway to get the pop up window in the same tab that I click the link

    • don’t use target=”_blank” on you link?

  13. bilogate

    thank u very much i have been trying abouth 2 hours :)

  14. does this work for you in all browsers? i’ve got it only working in chrome but not ie and firefox

    • ah it’s like that per default in chrome, so i’m not sure what i made wrong, nothing but that in the .htaccess?

  15. Just perfect :-)

  16. Pavel Valencia

    Hi!

    I test this trick, and work perfect in Chrome 15, Firefox 7, but in IE 9 this broken… show many invalid characters.

    Searching more info found this code:

    
    **FilesMatch "\.(mov|mp3|jpg|pdf)$"**
      ForceType application/octet-stream
      Header set Content-Disposition attachment
    **/FilesMatch**
    

    Please replace ** by as appropriate, for some reason in the comments do not see these characters.

    Test again and voila work in Chrome, Firefox and IE. In FF and IE show “save as…” dialog box, but in chrome download directly. How to fix this last issue? Force to Chrome show “save as…” dialog box…

    Greetings from Peru

    Pavel

  17. nashoo

    Solution from Pavel Valencia works. (I tried many other advices)

  18. thanks a lot for this tip!

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~