Force File download Using PHP – Do Not Display Open In Browser onclick

by Hiroshi on October 28, 2008

in htaccess

In your HTML directly link to the file which is to be downloaded.
i.e.

<a href="........./files/somefile.pdf">Download Pdf</a>.

You will get a pop-up box asking whether you want to save the file or open it normally.
Now you want to download it so open .htaccess file and write there some lines according yo your requirement mentioning extension of files which you want to download directly.

Place the following code into your .htaccess file.

AddType application/octet-stream .pdf
AddType application/octet-stream .doc
AddType application/octet-stream .csv

This script will force download all files having extensions .pdf, .doc or .csv.

.htaccess File

.htaccess files (or “distributed configuration files”) provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.

There is .htaccess file present by default at the root directory of your server. If not, you can always create a file named as .htaccess, code it and upload it in the desired directory or at the root level of your website.

In several web servers (most commonly Apache), .htaccess (hypertext access) is the default name of directory-level configuration files. A .htaccess file is placed in a particular directory, and the directives in the .htaccess file apply to that directory, and all subdirectories thereof. It provides the ability to customize configuration for requests to the particular directory. The file name starts with a dot because dot-files are by convention hidden files on Unix-like operating systems.

Wikipedia information on .htaccess

Related Posts

Previous post:

Next post: