How do I force a file to be downloaded by a web browser instead of displayed?
If you link to files on your website using code like this:
<a href="https://www.example.com/file.mp3">Download MP3 file</a> <a href="https://www.example.com/file.txt">Download text file</a>
... you’ll usually find that the web browser plays or displays the media or file, rather than downloading it to the visitor’s disk.
That's what you want in most cases, but you can force it to be downloaded by adding a Content-Disposition header for that filetype.
To do that, add lines like this to your .htaccess file:
<Files "*.mp3"> Header set Content-Disposition attachment </Files>
<Files "*.txt"> Header set Content-Disposition attachment </Files>
How can I learn more?
The Mozilla website has a page with more information about Content-Disposition headers.
Copyright © 2000-2022 Tiger Technologies LLC