Forcing the Download Prompt for files

Posted by on October 24, 2011 in Apache, Web Servers | 0 comments

Sometimes you want to force a file to be downloaded instead of letting the browser decide how to handle it. For alot of servers, text files, for instance, will be displayed on the screen as a bland text page with no formatting. Instead you may want the download prompt to come up, have the user download the file, then have them continue on their way on your website.
To accomplish this simply add an .htaccess file to the directory that the file you want to download is in. For instance, the following script will have any file type with an extension download instead of letting the browser process it.

<Files *.*>
ForceType application/octet-stream
Header set Content-Disposition attachment

The *.* section is what matches to files. So if you only want text files to download, use *.txt
Please note that .htaccess files may not work if your server has increased security measures. Check with your hosting service to see if .htaccess files are allowed if you are having issues with this script.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment

Your email address will not be published. Required fields are marked *