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.


<?php
$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));
ob_clean();
flush();
readfile($file);
exit;
}
?>

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

Hope helpful

Advertisements

About shafiqul
I am Shaikh Shafiqul Islam.I am working as a software engineer at Premium Web Cart in Bangladesh. I had completed my B.SC majoring Computer Science and Engineering in 2010 at the Dept of CSE in Ruet(Rajshahi University of Engineering & Technology), a leading engineering university of Bangladesh. I want to explore myself with latest technology and also want to develop my Career with well-established and fast growing IT Industry in Bangladesh. I started this blog to keep all the interesting PHP, HTML,Javascript,Ajax, Jquery related information.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Resource

Web Development Resource

UI and UX Design

User Interface and User Experience

Tutorial

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

%d bloggers like this: