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


  1. Eddie
    Permalink to comment#

    make sure to put this poop in .htaccess

  2. Michal Kopanski
    Permalink to comment#

    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
      Permalink to comment#

      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 for more information about php and headers.

    • Andy
      Permalink to comment#

      Hi Michal Kopanski,

      Did you ever get this figured out? I also want to put 2 links on my webpages. One to view the pdf in the browser and one to force the download.

  3. Greg
    Permalink to comment#

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


  4. Andy
    Permalink to comment#

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

  5. Gilank

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

  6. sahil
    Permalink to comment#

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

    • llort
      Permalink to comment#

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

    • Keith Hague
      Permalink to comment#

      Now then Ilort, that’s not very helpful. One doesn’t just become a webmaster overnight, it’s a learning process. How would you feel if your teacher said “don’t try to learn until you have learnt”?

      Gilank, if your site doesn’t have an .htaccess file just make one in a text editing programme.

      Now then, that wasn’t too hard was it.

    • Larry
      Permalink to comment#

      .htaccess is a file with no “name” in front of the “extension,” as seen in Microsoft products and Windows OS. It is actually a hidden file in Linux, so designated by the leading dot.

      That said, create a text file in Notepad, and fill it with the material presented at the top of this page. Name it with quotes: “.htaccess”

      It will save properly in the Windows OS if the name with its leading dot is in quotes in the Notepad Save dialog. Upload this file to the web root of your site next to your index.html (.php, .cgi, etc.) page which opens when someone dials up your website without specifying beyond the “”

      Your host may not allow the use of .htaccess, and has the server configured not to look for it, even if it is an Apache server in Linux.

      I run Apache on Windows for my local testing, and Apache does use .htaccess as if it were on Linux. Save your perl.exe on the same drive with the server, as \usr\bin\perl.exe, and your Perl scripts will work as if they were looking at the Perl interpreter at /usr/bin/perl.

      I prefer to answer people’s beginner questions with something other than “get out of it until you know what you’re doing.” That was rude of the other person who so replied.

  7. Uwe
    Permalink to comment#

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

    • Alex
      Permalink to comment#

      Where I have to include this code? To the css, js or php? Thanks!

    • Marcel
      Permalink to comment#

      Alex: Add it to the .htaccess file on your Apache server.

  8. srinivas
    Permalink to comment#

    thanks a lot!!!

  9. Nate Hamilton
    Permalink to comment#

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

  10. Yann Bouschet
    Permalink to comment#

    Thank youy so much for that trick.

  11. Salid
    Permalink to comment#

    great post /…… :)

  12. Federico González Brizzio
    Permalink to comment#


    Some people use:

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

    Do you know wich’s better?


  13. John
    Permalink to comment#

    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

  14. bilogate
    Permalink to comment#

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

  15. sinit
    Permalink to comment#

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

    • sinit
      Permalink to comment#

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

  16. Sven
    Permalink to comment#

    Just perfect :-)

  17. Pavel Valencia
    Permalink to comment#


    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

    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


    • The Mighty
      Permalink to comment#

      I did as your posted, Opera & Firefox did good but my IE still open the PDF as page not donwload it. Any advice ?

    • Paul
      Permalink to comment#

      This worked perfectly for me. Thank You!

    • Jake

      This worked for me too. The AddType wasn’t working in Chrome on Mac (didn’t test anything other than chrome and safari).


    • Rahul

      Works! For Me, For Our Mp3 Files..Thank You Very Much!

  18. nashoo
    Permalink to comment#

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

  19. Izrada web stranica
    Permalink to comment#

    thanks a lot for this tip!

  20. The Mighty
    Permalink to comment#

    Thanks, this tip works great on Opera & Firefox but not on my IE. I use Win XP SP2 and my IE version is 5.2.
    I also tried Pavel Valencia’s tip but it still doesn’t work for my IE.
    Any advices will really appreciated.

    Note: I’m using WordPress.

    Thank you!!

  21. Danny
    Permalink to comment#

    Pavel Valencia’s solution works great. Thanks!
    (notice that you need to change the ** with &lt and > like HTML tags

  22. Danny
    Permalink to comment#

    &lt should be <

  23. Sam Miller
    Permalink to comment#

    Does anyone know how to make this work for mobile? I’ve tried both Chris’ solution and Pavel’s, however mp3s still stream rather than download on mobile devices. Any ideas?

  24. Ramesh
    Permalink to comment#

    can you give a example for this how I can link a pdf file to download.

  25. vasu
    Permalink to comment#

    where add the above code

  26. LIU
    Permalink to comment#

    FTP to your site and create a file named .htaccess in your files directory. In that file put the following lines:

    Header set Content-Disposition attachment

    This particular example will force files with doc, mov, pdf, rtf, or txt to download instead of being shown in the browser.


  27. LIU
    Permalink to comment#

    ughh.. looks like this comment area doesn’t render the code.

    Just take a look at:
    Scroll down to the Force File Download section.

  28. rudolf
    Permalink to comment#

    it didn’t work for Chrome 17 for Mac…

    I came back to post the solution that worked, but it’s almost the same as Pavel Valencia published above

    I’m not sure how to mix both, because this one does prevent lower/uppercase issues

    <FilesMatch "\.(?i:pdf)$">
      ForceType application/octet-stream
      Header set Content-Disposition attachment
  29. Chosen 1 Design
    Permalink to comment#

    Thanks ! This was just what I was looking for and very helpful. Gotta love .htaccess files.

  30. Vernard
    Permalink to comment#

    If you’re getting Internal Server error after posting the HTACCESS codes in .HTACCESS document… is there anything amiss? BTW, the website is a WordPress system.

    • Mohamed
      Permalink to comment#

      For internal Server error : you must enable header module ->
      cd /etc/apache2/mods-available
      a2enmod headers
      service apache2 restart

  31. Alex

    Pavel Valencia’s version works like a charm! Thanks

  32. Pranav

    I have created .htaccess file , and applied code as ” AddType application/octet-stream .txt ” , but still the file is opening in browser , it don’t allows to downloading the file.

  33. Andrés Guevara
    Permalink to comment#

    Hi, I don´t know why but this doesn´t work with Google Chrome. I don´t have a problem with any other browser. Is it only with me? or anybody else has the same problem?

    • Lindy Dobbins
      Permalink to comment#

      Have you cleared your cache in Chrome? If you have been testing the same file by the same name, you ‘ll need to do that before it will work.

  34. Alysha Hayse
    Permalink to comment#

    Ok, I’ve been searching ALL DAY for an answer to this, I’m not sure i’m in the right thread but I’m going for it anyway! I’m running on a Windows 7 computer. I just purchased a digital scrapbook paperpack. She says she sent me the 12 individual files in a zipped folder. However, when I downloaded it it appeared in my downloads folder as a blank piece of paper. It appears to be a octet-stream file and none of my programs will open it. I have no idea what to do with it. She then sent me the individual files, unzipped as JPEGS. When I downloaded them the only thing I could open them in is Windows Media Gallery. When I tried to rightclick>copy>paste into a specific folder it appeared again as just a blank piece of paper and am still unable to open it in anything. (Photoshop is where I’m trying to use it) ANY suggestions would be GREATLY appreciated!

    • Deepak
      Permalink to comment#

      Response.AddHeader(“Content-Disposition”, “Attachment;filename=somefile.ext”);

      This will help you

  35. Mustafa Contractor
    Permalink to comment#

    this is so very great…. thanks a lot!

  36. Craig
    Permalink to comment#

    Hi – I know this thread has been going for a while and I hate to kick a dead horse, but we are running into trouble with iOS devices.

    Has anyone found a solution that works for Mobile Safari? The htaccess adjustment works great for desktop browsers, but iOS devices still stream MP3s.

    Any suggestions?

    • rudolf
      Permalink to comment#

      You’d better take this question to StackOverflow and report back with the answers :)

    • Chris
      Permalink to comment#

      Where would it download to? iOS doesn’t have an accessible file system.

    • Robert Piller
      Permalink to comment#

      Hey Craig,

      Did you find an answer to this? I wish to prevent streaming on iOS devices. I understand there’s no filesystem, so it obviously won’t download, but I’d at least like to prevent streaming! And I would like to allow downloads on mobile devices that can download, like Android phones.

    • Larry
      Permalink to comment#

      To prevent streaming on devices that cannot download, perhaps sniff for device user-agent strings and serve alternate content to devices that want to stream.

  37. Alberto
    Permalink to comment#

    Great trick!
    But I have still a problem with video files (.mov, .mp4)
    it seems it starts download the file and immediately opens quicktime.
    but with large size files quicktime can’t open properly.

    Is there a way to force the whole download?


  38. Craig
    Permalink to comment#


    That’s the problem. It doesn’t download. But nekkid MP3 links play on an iOS device. And with several thousand people accessing the several 50MB files for each product our server is under severe strain – maxing our bandwidth @ 1&1. We considered off-loading the files, but the bandwidth costs at Amazon would be more than our revenue…

    We need a way to prevent iOS devices from automatically playing nekkid MP3 links. Currently we wrapped ’em in a ZIP file but that creates a new set of ‘how do I unzip files?’ problems.

    Any suggestions?

    • Larry
      Permalink to comment#

      Will server environment variables store iOS user-agent strings? Sniff for the offending user-agent and serve alternate content in place of the streaming files.

  39. Hany
    Permalink to comment#

    Thanks. worked for me. I hope future HTML version allow such an option.

  40. LK
    Permalink to comment#

    I couldn’t get Chromium/Chrome to respond to AddType application/octet-stream .pdf so instead I used htaccess to send the Content-Disposition: attachment header as Chromium Issue 142947 seemed to indicate was preferred. However, this still didn’t work. It seems Chrome interprets Content-Disposition rather strictly, as detailed here by Foo Compels You. Having uppercase characters in my filename caused Chrome to open the file in the in-browser PDF viewer but changing these to lowercase caused the file to download, as intended. Interesting how letter case affects things. Hope this helps.

  41. Scott H
    Permalink to comment#

    I would not use this approach as it currently prevents Chrome users from downloading the file.

    A link to a PDF it will not do anything when a Chrome user clicks on it.

    Chrome 24 – BAD – link does not work at all – user cannot access the file
    Firefox 17 – GOOD – works as expected
    IE 9 – OK – It downloads the file then opens it with the PDF viewer

    I would use the FilesMatch approach one of the other commenters noted – it doesn’t prevent any of your users from getting the file.

    <FilesMatch “.(?i:pdf)$”>
    ForceType application/octet-stream
    Header set Content-Disposition attachment

  42. Grafit
    Permalink to comment#

    thx for this tip..

  43. Lauren
    Permalink to comment#

    I’ve asked our resident programmer to force our PDF to download, and I received this response, of which, being a graphic designer, not a programmer, I understand pretty much nothing. Is he correct and am I asking for something particularly complex?:

    “I know what it takes to force a browser to download rather than
    display. In fact I have some pages that do just that. It’s a
    server-side issue, because you have to specifically populate the
    response header with an “application-octet” stream directive, along with
    filename. If you know a special way of doing it client-side, via HTML,
    CSS, and/or javascript that all browsers understand in the same way, by
    all means let me know and I can put it up. Otherwise it takes hours to
    get the server to do it just right. Hours that I won’t have for at
    least a few weeks.”

    • Ramiro
      Permalink to comment#

      <a href="link" download>download this file</a>

      But for the moment is only working in chrome

  44. James Williams
    Permalink to comment#

    I have been using a series of google drive accounts for download for a 300 meg .exe
    as I had no idea on how popular it would be.

    I wanted users to stay on the page, so all I did was add “target=”_top”> to the URL.

          <a href="your url to the file" target="_top">

    My download link is a graphic, but the latter does me fine.

  45. Matt
    Permalink to comment#

    Thanks for this tip. I wouldn’t even have thought of using htaccess for this. Glad you were so willing to share!

  46. Justin
    Permalink to comment#

    Hey all,

    I made a .htaccess file in the top-most folder (world wide web/sitename/.htaccess) of my site and added the AddType and FilesMatch codes but neither of them seem to work.

    I am using Joomla! 3.0 for my companies intranet page and we have a TON of PDF’s that need to be force downloaded. There is no extension that can do this that I know of.

    Any other ideas or am I doing something wrong?

  47. April
    Permalink to comment#

    Hey guys can someone leave a code?! i really dont understand how to do that :(

  48. Marko
    Permalink to comment#

    Now you can also do this with HTML5.

    Source (Click)

    • Giovanni Pires da Silva

      Wow! Awesome Marko! I didn’t know that it was possible through the HTML5. Very cool! Thank you for sharing it.

  49. steve


    I have a client who wants to do the opposite. Some browsers open, some download.
    Can you override this and force the browser to open in a new window!?
    Or is that a really silly question?


  50. myrna
    Permalink to comment#

    This has been a really helpful thread, and Rudolf’s version works great. What I like is that Firefox, IE, and Safari give the user the option to open or download. Only Chrome forces the user to download, which I am afraid is going to confuse some of my less tech-savvy end users. Is there a way to “force” the “open or download” option in Chrome?

  51. Narender
    Permalink to comment#

    Oh man! First result in Google and this is it. Thanks a lot for this post. Solved all of my problem. However still testing if the result is the same with All main browsers ans well as mobile browsers :)

  52. Tom
    Permalink to comment#

    Do not work in Chrome version 30.0.1599.69.

    Tried solutions from comments as well – any hints?

  53. ArjayBerg
    Permalink to comment#

    Best way to do that is this:

    Download PDF

    –a href=”” download— Download PDF –/a—

    Replace –to < open tag

    Replace —to > close tag

    You’re welcome ;)

  54. Haary
    Permalink to comment#

    I need to download .sql file in a folder. I have a button named download. And button has it’s own value. ie.

    (sql filename action)
    (1.sql ——- download,
    2.sql ——– download)

    when clicking on download button

    if(isset($_REQUEST[‘download’])){echo $_REQUEST[‘download’]; //result either 1.sql or 2.sql}
    1.sql,2.sql are in the same folder in which my download.php file is placed.

    This work is for my word press site.My need is to download 1.sql or 2.sql to my computer when corresponding button is clicked.


    if(isset($_REQUEST[‘download’])){download file.( $_REQUEST[‘download’]).

    This is for a new plug in .

    i write code if(isset($_REQUEST[‘download’]))
    header(“Content-Type: application/octet-stream”);
    header(“Content-Disposition: attachment; filename=\””.$filename.”\””);

    Warning: Cannot modify header information – headers already sent by (output started at H:\xampp\htdocs\yu\wp-admin\menu-header.php:179) in H:\xampp\htdocs\yu\wp-content\plugins\yu-DB-Backup\inc\inc.wpgc_db_backup.php on line 6

    Warning: Cannot modify header information – headers already sent by (output started at H:\xampp\htdocs\yu\wp-admin\menu-header.php:179) in H:\xampp\htdocs\yu\wp-content\plugins\yu-DB-Backup\inc\inc.wpgc_db_backup.php on line 7

    Warning: readfile(http://localhost/yu/wp-content/plugins/yu-DB-Backup/2.sql) [function.readfile]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in H:\xampp\htdocs\gcertificate\wp-content\plugins\WPGC-DB-Backup\inc\inc.wpgc_db_backup.php on line 8 .
    Please help me if anyone know the answer

  55. Matt
    Permalink to comment#

    Is there anyway to have this only for one specific pdf vs all pdfs on the server?

  56. moe
    Permalink to comment#

    Which directory should I place the .htaccess file with this line in it: “AddType application/octet-stream .csv”

    Where my csv file is or where my php scripts are?
    Using WordPress.


    • Larry
      Permalink to comment#

      Put your .htaccess in the directory containing the CSV file. Here the .htaccess is only in effect in this directory and any subdirectories, and not on “sibling” or “parent” branches of your directory tree.

      You may place the .htaccess file in the parent of this directory, and it will work equally well here, but will also affect all subdirectories under the parent.

      I do not know about WordPress, as most of my site development work has been in HTML or PHP, but this would be my first inclination of placement.

  57. Caleb G
    Permalink to comment#

    If you want to force all files in a directory to download, create a .htaccess file in that directory, then paste this in it:

    ## force all file types to download if they are in this directory:
    ForceType application/octet-stream

    Don’t stick this in your root level .htaccess file. It will cause all the files on your server to download.

  58. Hoang Minh
    Permalink to comment#

    Hope helps you!

    “pdf” => “application/pdf”,
    “txt” => “text/plain”,
    “html” => “text/html”,
    “htm” => “text/html”,
    “exe” => “application/octet-stream”,
    “zip” => “application/zip”,
    “doc” => “application/msword”,
    “xls” => “application/”,
    “ppt” => “application/”,
    “gif” => “image/gif”,
    “png” => “image/png”,
    “jpeg”=> “image/jpg”,
    “jpg” => “image/jpg”,
    “php” => “text/plain”,
    “csv” => “text/csv”,
    “xlsx” => “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”,
    “pptx” => “application/vnd.openxmlformats-officedocument.presentationml.presentation”,
    “docx” => “application/vnd.openxmlformats-officedocument.wordprocessingml.document”


  59. Deg
    Permalink to comment#

    You can also use a addon for firefox or a service like to download, but not open some direct file link.

  60. Flimm
    Permalink to comment#

    Firefox, Chrome, Opera, Android Browser and Chrome for Android support the download attribute on links, mentioned by a couple of other commentators:

    <a href="example.pdf" download>Example 1</a>
    <a href="example.pdf" download="different_name.pdf">Example 2</a>

    See the entry for download.

  61. Danny
    Permalink to comment#

    Thank You!

  62. Al
    Permalink to comment#

    Caleb G – thanks – that worked for me.

  63. Bob Thompson
    Permalink to comment#

    Can someone please tell me how to set up my FTP so that files are opened in the browser and NOT downloaded?

    I seem to have the opposite problem everyone else has. I want my files to be displayed in the browser and not downloaded. Everything I’ve tired to make that happen has failed.

    Someone suggested setting up a .mtaccess file in my /var/www/html directory, but the only examples they gave are to force downloads.

    Thank you.

    • Qfla
      Permalink to comment#

      Bob, did you ever get anything to work. I have exactly the same problem and thought this was a past i had made until I saw your name

  64. Rob
    Permalink to comment#

    A nasty solution, this makes the server pass the incorrect mime type to the browser, tricking it into treating the data as an unknown format. Better educate your users and explain a browser can save any file,.

  65. Elad Karako
    Permalink to comment#

    Nice solution, I’ve added some proper filename matching,
    anti-mimetype sniffing and HTML5 solutions too (may combined)

    ##place inside a folder that contains some music videos and documents, this will override the default handler.
    <FilesMatch "\.(mov|mp3|pdf)$">
      <IfModule mod_mime.c> 
        ForceType application/octet-stream
      <IfModule mod_headers.c> 
        ##fix a bug in old GoDaddy servers.
        Header unset X-Content-Type-Options
        Header unset Content-Disposition
        ##prevent mimetype sniffing (first few bytes can determine that a file should be opened in browser).
        Header set X-Content-Type-Options "nosniff"
        ##extract filename, apply to proper (for example) Content-Disposition: attachment; file="my music.mp3"
        SetEnvIf Request_URI "^.*/([^/]*)$" FILENAME=$1
        Header set "Content-Disposition" "attachment; filename=\"%{FILENAME}e\""
        UnsetEnv FILENAME
  66. Godwin
    Permalink to comment#

    I am using this to force download .log files. Issue is when the logs files get rotated. Example: .log0001, .log0002 or .log12345
    Any wildcard that I could use to download file extensions starting .log?

    • Larry
      Permalink to comment#

      You can use a Perl server-side script. It has been awhile since I’ve been involved with Perl, but it will tackle this job. PHP may also have the capability to do the same. You may also be able to use JavaScript on the client side as well.

  67. Manu
    Permalink to comment#

    I would like to point out, that all suggested solutions are not working if the user tries to download the file with a 1.0 – 4.3 Android operation system and the Chrome or stock Android browser.

    See this almost 5 years old bug report:

    Either you are ignoring this problem (should be fixed with KitKat), or you are informing your users to force download with long pressing the download button, or point out that there are other browsers for Android available, which none of them struggling with this task.

    But don’t waste your time trying to fix this problem. There is no fix – both Chrome mobile and the stock Android browser are using the build-in Android download manager to download files – and as a web developer you are unable to get this very download manager to do it’s job right.

  68. Gabriel O
    Permalink to comment#

    Please, I need assistance I had tried several method but all is to no avail.
    here is my code;

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

We have a pretty good* newsletter.