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.

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

  1. I am microsoft certified technician ,i provides support to microsoft,support for microsoft,microsoft support,microsoft tech help,
    microsoft online help , any one can contact us on our support to microsoft toll free,
    microsoft technical support number,microsoft support tollfree number,microsoft customer tollfree number,microsoft service tollfree number
    we also provides support for various Microsofts Product like support to ms office,support windows 10,windows 10 support, windows 10 support toll free number,windows 10 online support,windows 10 tech support number

  2. Watching your favorite movies and tv shows have become so easy these days, thanks to some great apps like ShowBox. It allows you to watch movies in multiple resolutions according to your preference and it is absolutely free as well.
    Just check out my site to know how to install ShowBox on your smartphones
    ShowBox for PC

  3. Very useful post thanks for the sharing here
    Here is the main update about the CG Board 10th result 2018 for the students who are going to appear 10th examinations in the academic year 2017-18 of Chhattisgarh state. CGBSE is likely to release the CGBSE 10th Result 2018 on April 2018 and the details of exact date, where and how to check the results and more information about it are given here. Reading this completely article can give you the entire details on CG board 10th exams, results and more.

  4. We are soon going to have the first ever iOS 12 update for our iPhone and iPad devices. People are interested in knowing about iOS 12 release date. Here we have covered this topic in detail on our website.

Leave a Reply

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