Download any type of file,image,pdf using PHP Script

Sometime in our website , need a download link. When click this link then  any type of file,image, pdf  will be download.You can do this using a simple php script.

First Create a simple html file where you want to add download link.

<a href="download.php?file=shafiq_photo.jpg">Download Photo</a>

i want to download a picture name “shafiq_photo.jpg” so parameter is file name “shafiq_photo.jpg”.

Then Create a php file  name “download.php” which you use in above file.

$file = $_GET["file"];

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . urlencode(basename($file)));
// header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));

Important thing is that  picture and script  same folder if you change this then you can use your folder directory.

Hope helpful


About shafiqul
I am shafiq, i have passed B.Sc. in Computer Science and Engineering. 2010 from Rajshahi University of Engineering & Technology, Rajshahi. CGPA: 3.30 out of 4.00.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Web Development Resource

UI and UX Design

User Interface and User Experience

php tutorial

code,Tips,Tricks in php,mysql,javascript,jquery,ajax,yii,codeigniter,laravel,drupal,slim,bootstrap,api,jQuery Mobile

%d bloggers like this: