Wednesday 26 September 2018

PHP script for downloading files

As we see on many websites, there are file download links. When we click on the link, browser starts downloading. The question arises that can we do this ? of course, let's learn how to download a file with php. Suppose we have a pdf file which we want to allow a user to download. Here is simple coding for downloading a file with php. Downloading of file is done by sending different headers to browser and readfile function. In headers, we send information about file like type of file, file size, name etc. Make file download.php and write given coding in it.


<?php
//download.php
$filename="sample.pdf";
$file="c:\\myfile\\$filename";
$len = filesize($file); // Calculate File Size
ob_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public"); 
header("Content-Description: File Transfer");
header("Content-Type:application/pdf"); // Send type of file
$header="Content-Disposition: attachment; filename=$filename;"; // Send File Name
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len); // Send File Size
@readfile($file);
exit;

?>
Basic Php script for downloading a file coding is completed, our next step is providing download link in page like <a href="download.php">Download File</a>. So this was demo for pdf file download through php. In next step we are going to learn that how can we make common php script to download different kind of files? We are going to make common php function for download different kind of files.

<?php
   // function.php
 function download_file($filepath)
 {
  if (!is_file($filepath)) 
     {
    echo("404 File not found!"); // file not found to download
    exit();
     }
  
     
   $len = filesize($filepath); // get size of file
   $filename = basename($filepath); // get name of file only
   $file_extension = strtolower(pathinfo($filename,PATHINFO_EXTENSION));
   //Set the Content-Type to the appropriate setting for the file
   switch( $file_extension )
     {
     case "pdf": $ctype="application/pdf"; break;
     case "exe": $ctype="application/octet-stream"; break;
     case "zip": $ctype="application/zip"; break;
     case "doc": $ctype="application/msword"; break;
     case "xls": $ctype="application/vnd.ms-excel"; break;
     case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
     case "gif": $ctype="image/gif"; break;
     case "png": $ctype="image/png"; break;
     case "jpeg":
     case "jpg": $ctype="image/jpg"; break; 
     default: $ctype="application/force-download";
    }
    ob_clean();
    //Begin writing headers
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public"); 
    header("Content-Description: File Transfer");
    //Use the switch-generated Content-Type
    header("Content-Type: $ctype");
    //Force the download
    $header="Content-Disposition: attachment; filename=".$filename.";";
    header($header );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$len);
    @readfile($filepath);
    exit;
  }
Now you can use above function to download pdf,jpg,doc and excel files etc. you need to give full path of the file rest function will take care of all. To use above function make a php file with name download.php and include above file function.php in it. And give full path of file which you want to download as parameter to download_file() function like given below.
 <?php
// download.php
 $filepath = "c:\\myfile\\test.doc";
 download_file($filepath);
In last step provide download link in page like <a href="download.php">Download File</a> where you want.

0 comments:

Post a Comment