Force Files to Download and Not Open in Browser Using Apache or PHP

By default most of the file types (eg: pdf, csv, txt, mp3, mov, mp4, jpg, png, gif, html, etc.) displayed in browser instead of download. But we can force browser to download these files instead of showing them. In this article we will explain how to force file download using either Apache or PHP.

Using Apache

Using Apache and .htaccess you can use AddType application/octet-stream to force the download of multiple extensions:


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

The MIME type application/Octet-stream is considered to be one of the popular multipurpose application files. Generally this type is used for identifying the data that is not associated with any specific application. An Application/Octet-stream is a MIME attachment which is present in the operating system.

Using PHP

PHP allows you to change the HTTP headers of files that you’re writing, so that you can force a file to be downloaded that normally the browser would load in the same window. This is perfect for files like PDFs, document files, images, and video that you want your customers to download rather than read online. Using PHP you can take advantage of the PHP built-in readfile function:


$file_url = 'http://www.example.com/file.pdf';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
exit();

Line two forces browser to download file by defining the Content-Type. The fourth line sends the filename to save using Content-disposition. Finally the fith line outputs the file content to begin the download stream.

Victory

Using either of these techniques your files will now be forced to download.

7 thoughts on “Force Files to Download and Not Open in Browser Using Apache or PHP

  1. Your search for Madden NFL Mobile Hack STOPs here https://highlightstory.com/make-everything-easy-with-madden-mobile-hack/.

    After the great success of our previous release we present new brand new Madden NFL Mobile Hack using which you can generate unlimited number of resources absolutely free of charge.

    When you visit above site you don’t need to download anything to your computer.There are some fileds like your game username and the amount of Coins, Cash and Stamina that you want to generate on your game account.

  2. Thank you so much for this wonderful Post and all the best for your future. I hope to see more posts from you. I am satisfied with the arrangement of your post. You are really a talented person I have ever seen.

  3. Koyal.Pk has created an Urdu song playlist for you where you can enjoy listening to mp3 music online. However, you can listen to new Urdu songs by popular and trending artists that may include a variety of songs or mix collections of music in various categories such as wedding, dance, sad songs, love & romantic songs, and retro tracks.

Leave a Reply

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