Trigger to Download a File When Clicking Link
There are two ways to trigger to download a file.
- HTML 5 Download Attribute.
- 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
<a href="SampleFile.pdf" download>Download File</a>
If you want to rename the file at the time of downloading, use the following code:
<a href="SampleFile.pdf" download="new_filename">Download File</a>
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:
<a href="download.php?file=SampleFile.pdf" target="_new">Download File</a>
PHP Script
if (isset($_GET['file'])) {
$file = $_GET['file'];
if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) {
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
}
}
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
Twitter Official Page: All PHP Tricks
Hi! Your example works in my modded download form. But I have a question, I tried it by passing the file name in my form action:
…
Which works, but I need to hide the filename within the PHP, so I tried this:
if (isset($_GET[‘file’])) {
$file = $_GET[‘file.rar’];
if (file_exists($file) && is_readable($file) && preg_match(‘/\.rar$/’,$file)) {
header(‘Content-Type: application/rar-compressed’);
header(“Content-Disposition: attachment; filename=\”$file\””);
readfile($file);
}
}
But it doesn’t work. It just refreshes the page when you click the download button and nothing downloads. Can you assist? Thanks!
Great very helpful
i tried and its not working T_T
Try on the updated browsers.
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.
Well, i suggest you should look at my Ajax tutorials, hope this will help you to trigger download using ajax.
Check out ajax tutorials, how to use ajax with different examples https://www.allphptricks.com/category/ajax/
How can we download pdf file on button click in php?
Hi Hamza, both methods are explained in the above tutorial.
Very good job. It is so simple and easy to understand. Thanks a lot for sharing knowledge.
Thanks JAVED UR REHMAN, i have learned a lot.
You welcome Semer Nassir 🙂
Javed, your code are amazingly simple and straightforward. Thanks for being a promoter of success.
Thanks Ibrahim that you like my tutorial.
Good, thanks sir..
You welcome Michael, if you found it helpful. Share it on social networks.
Wow!! This really helped. Thanks a lot Javed.
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
Very good job. It is so simple and easy to understand. Thanks a lot for sharing knowledge
Well thanks that you like it, share it with others 🙂