0 0 Votes

HTACCESS: Force Download of specific File Types

Tutorial by Computer Expert | 16/09/2014 at 23:13

Depending on the browser settings, some file types such as images, videos, music, text files or PDF documents are always displayed directly in the browser window. This is a problem whenever you would like to offer a JPG or PDF file as a download: each time when clicking on the corresponding link, the download is not starting and the file is just displayed instead.

To change this behavior, you can just add the following to your HTACCESS-file:

<FilesMatch "\.(?i:pdf|jpg|gif|avi|mp3)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment

In this example, all files with the extension PDF, JPG, GIF, AVI and MP3 are declared as binary attachments. By this, the browser is offering the files as a download instead of falling back to the default display. Of course, you can change the list of extensions to your needs.

By the way, with using "?i:" we ensure that the lowercase or uppercase writings of the file extension does not matter (PDF and pdf).

The PHP Solution

Of course, this is a very simple method. If you want to have more control over your downloads, I recommend the PHP download script for files. This script also offers the possibility to force the browser to a file download, but you have more possibilities than only changing the header (for example the implementation of a download counter or downloading under another name than the name with which the file is stored on the server).


About the Author

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message


Related Topics

PHP: File Download Script
Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own articles on askingbox.com. How to do.