A Web Design Community curated by Chris Coyier

Code Snippets Gallery

Code Snippets > HTAccess > Force Files to Download (Not Open in Browser) Submit one!

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

10 Responses

  1. Eddie says:

    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!

    • Sam says:

      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. Greg says:

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

    Thanks

  4. Andy says:

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

  5. Gilank says:

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

  6. sahil says:

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

  7. Uwe says:

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

Leave a Comment

Remember:
  • Be nice.
  • Wrap multiline code in <pre> and <code> tags and escape it first (turn <'s into &lt;'s).
  • You may use regular HTML stuff like <a href="">, <em>, and <strong>
* This website may or may not contain any actual CSS or Tricks.