How to force a file to download (Not open in a browser)

Modern browsers have very good file handling capabilities but there are times when you would prefer to force a file to download rather than have it opened in the browser. To achieve this you need you need to override what the browser is trying to do and let your operating system handle the file. This is easy to do with a simple code within your .htaccess file.

AddType application/octet-stream .[filetype]

Example

For example, as standard the browser is able to handle text files and it would take over the page that you are currently viewing, but by by inserting the code below when the link is clicked you will receive a window from your operating system asking where to save the file rather than showing the file in the browser window.

<IfModule mod_mime.c>
AddType application/octet-stream .txt
</IfModule>

Which files would you want handled by your operating system?

The example of files that you would like to download are:

  • Microsoft Office Documents: Word, Excel, PowerPoint [.doc,.docx.xls,.xlsx,.ppt]
  • Comma separated Values [.csv]
  • Text Documents [.txt]
  • HTML file as a download [.html,.htm]
  • Portable Document Format [.pdf]
  • Movie files to download rather than play [.mpg,.avi,.mov]

How to force a Web Page to Download

Your browser is designed to handle web pages; In fact this is it's primary function! So what do you do when you want to offer a web page as a download?
One of the beautiful things with .htaccess files is that it will override the standard server configuration for the folder AND any subfolders that it is placed. This means that if you specify for a filetype to download in your /public_html/ folder this will affect your whole site, so to get around the issue that you want your site to display normal html webpages, the download must be put into a separate subfolder. Within your cPanel web hosting package create a folder called /public_html/downloads/ and in the .htaccess file within that folder add this code:

<IfModule mod_mime.c>
AddType application/octet-stream .html
AddType application/octet-stream .htm
</IfModule>

I hope this helps and if you have any question please let me know if the comments below

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Do you support SSI (Server Side Includes)?

Yes we do. SSI (Server Side Includes) are tags that are placed in HTML pages, and evaluated on...

How can I change the file name extension of my index page?

It is possible to name your default index page something other than index.html, index.cgi,...

How can I enable directory indexing

If you want to see a list of files in a directory when viewing it in a browser rather than a web...

How can I force a particular charset? (Character Encoding)

You are able to set a default value for the media type charset parameter (the name of a character...

How do I edit php.ini (PHP settings) for my hosting package?

Becuase of the way in which we run PHP, you are able to customise its behaviour with php.ini...