Force a File to Download Instead of Showing Up in the Browser

Force a File Download

Force a File Download

Usually when a user goes to a file URL (for example: a download link), the file will show in the browser if the browser have a method to display that type of file.For example if you open an image link in your browser it will always display.Few extensions like .ZIP ,.EXE will be always downloaded because your browser doesn’t know how to read that type of files.A simple solution to  force a file to download instead of showing up in the browser is to zip these files.But it is not always possible to zip these files due to limitations of server resources etc.

If you want to force a file to download instead of showing up in the browser then you can use any of these methods.

1).Changing .htaccess file-If you want to force all PDFfiles to download instead of showing up in the browser you can use these codes.

Similarly you can customize these codes for .jpg and other file extension.

2).Add an extra header-You can add an extra header using your server-side programming language.Content-Disposition header is used to force a file to download instead of showing up in browser.In php you can set this header in following manner .

Entire code to force a PDF file to download can be written as follows.