MYKEMINI
Hello friends this post is about Download Script to Download any File using PHP. So in this you will learn how to download a file in PHP but also you need to know this download actually force the browser to download file. But this is very easy simple few steps you just have to follow this code.
In below download script, these are mainly two functions are used one isheader()and another function isreadfile()which I will describe later. Using these function you are able to download any file. And this type of download code we have used most of the web application.
In this post we will see two types of download button, which works same. One download button is Filename with link to download it means when you hover over the mouse pointer to download button you will see the file name. Another download button won’t see any file name or path.
Theheader()function describe sends a raw HTTP header file to the client side. And It is also very important thatheader()must be called before any output is sent.
Thereadfile()function reads and writes a files to the client side. This function returns TRUE or FALSE and an error on failure.
Using these functions with script you can download any file very easily. This script is also very simple, easy to understand. So lets start with simple example with live demo..
» Download Link With Filename
» » index.php
<a href ="download.php?filename=logo.jpg"><span>Download Link With Filename</span></a>
Copy code
» »download.php
<?php
$file = $_GET['filename'];
$download_path = 'file/'.$file;
$file_to_download = $download_path; // file to be downloaded
header ("Expires: 0");
header ("Last-Modified: " .gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-store, no-cache, must-revalidate");
header ("Cache-Control: post-check=0, pre-check=0", false);
header ("Pragma: no-cache"); header("Content-type: application/file");
header ('Content-length: '.filesize($file_to_download));
header ('Content-disposition: attachment; filename='.basename($file_to_download));
readfile ($file_to_download);
exit;
?>
Copy code .....................................................(Continue)...
MYKEMINI
MYKEMINI » » css
<style>
.main_area{ margin:0 auto; width:400; text-align:center; margin-top:200px;}
.main_area a{ text-decoration:none;}
.main_area span{ font-size:25px; background:#df4662; color:#FFFFFF; padding:5px 10px; border:1px solid bc344d; border-radius:5px;}
</style>
Copy code
» Download Link Without Filename
» » index.php
<a href ="download2.php"><span>Download Link Without Filename</span></a>
Copy code
» » download2.php
<?php
$download_path = 'file/logo.jpg';
$file_to_download = $download_path; // file to be downloaded
header ("Expires: 0");
header ("Last-Modified: " .gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-store, no-cache, must-revalidate");
header ("Cache-Control: post-check=0, pre-check=0", false);
header ("Pragma: no-cache"); header("Content-type: application/file");
header ('Content-length: '.filesize($file_to_download));
header ('Content-disposition: attachment; filename='.basename($file_to_download));
readfile ($file_to_download);
exit;
?>
Copy code
» » css
<style>
.main_area{ margin:0 auto; width:400; text-align:center; margin-top:200px;}
.main_area a{ text-decoration:none;}
.main_area span{ font-size:25px; background:#df4662; color:#FFFFFF; padding:5px 10px; border:1px solid bc344d; border-radius:5px;}
</style>
Copy code