1Download File Using PHP – Server Side Script
2We 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:
3
4<a href="download.php?file=SampleFile.pdf" target="_new">Download File</a>
5PHP Script
6
7if (isset($_GET['file'])) {
8$file = $_GET['file'];
9if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) {
10 header('Content-Type: application/pdf');
11 header("Content-Disposition: attachment; filename=\"$file\"");
12 readfile($file);
13 }
14}
15The above file will read the file name and trigger to force download. This example will work on all browsers