Like Us on Facebook

Download Script to Download any File using PHP

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 is header() and another function is readfile() 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.

The header() function describe sends a raw HTTP header file to the client side. And It is also very important that header() must be called before any output is sent.

The readfile() 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 Script to Download any File using PHP uandblog


Download Link With Filename

index.php

<a href="download.php?filename=logo.jpg"><span>Download Link With Filename</span></a>


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;
?>


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>
 

Download Link Without Filename

index.php

<a href="download2.php"><span>Download Link Without Filename</span></a>


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;
?>


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>
 

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 0 )

    Leave a comment..

    Click to login.