Trigger to Download a File When Clicking Link

Demo Download

There are two ways to trigger to download a file.

  1. HTML 5 Download Attribute.
  2. Download File Using PHP Script.

The download attribute triggers a force download but it does not supported by Safari. Following are the versions that support download attribute.

Chrome = 14.0
Internet Explorer = 13.0
Firefox = 20.0
Opera = 15.0

Download File Using Using HTML 5 Download Attribute

If you want to rename the file at the time of downloading, use the following code:

Download File Using PHP – Server Side Script

We will need to create PHP script file and pass the file name in the href attribute that we want to download as you can see below:

PHP Script

The above file will read the file name and trigger to force download. This example will work on all browsers.

Note: I used this example for PDF file type, you can change it as per your requirement.

If you found this tutorial helpful so share it with your friends, developer groups and leave your comment.

Facebook Official Page: All PHP Tricks

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.

15 Comments

  1. Very nice. i implemented same things using same trick trough ajax but its not working, with PHP working when i run direct to file can you suggest me how can i implement with ajax.

  2. thank javed. you know I want to make a music website and I ask will this help or is there any suggestion based on that

Leave a Reply

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