How to make a download link for download file in php

How to make a download link for download file in php or create a download link for download file in php?
so here is the code and add base url according to your need
 
Create a php file download.php
and paste this code in you php file
$name = $_REQUEST['name'];
// The file path where the file exists
$filepath = 'http://www.site.com/files/'.$name;
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//setting content type of page
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($filepath ));
header("Content-Description: File Transfer");
//Read File it will start downloading
@readfile($filepath);

Now add this link in your file on your anchor tag
like <a href="http://www.site.com/download.php?name=file.png" >download link</a>

Comments