A Web Design Community curated by Chris Coyier

A little dab'll do ya

Code Snippets

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

Subscribe to The Thread

  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

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

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

  10. Salid says:

    great post /…… :)

  11. Federico González Brizzio says:

    Hi,

    Some people use:

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

    Do you know wich’s better?

    Thanks!

It's Your Turn

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 ---