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

About these ads

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:

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

skrwebdesigner

Designer Tutorial

php tutorial

Ephptutorial is created by pramodh,who shares code,Tips,Tricks in php,mysql,javascript,jquery,ajax,yii,codeigniter,laravel,slim,bootstrap,api,jQuery Mobile

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: