Force Download File in Php

To download a file using php you need two things.



  •  File name .
  •  The path of file you want to download.

             Here are two steps :


Step  1: 


Create a folder in your xampp and put a file which you want to be force download using php.In my case i am using myfolder as a floder and hey_baby.mp3 file in it.


  • Remember if you are using this code then your directory structure will be as follows.




Step 2:



  • Create a index.php file and create a anchor tag here so that on clicking on it you can excute the download.php script.This file can be understood by following code or you can copy and paste this code and save this file in your xampp with index.php

<html>
<head>
<title>Anchor for download file</title>
</head>
<body>
<h2> Click on download file </h2>
<a href="download.php?file=hey_baby.mp3"
style="padding:20px 24px;background:green;color:white;">Dowload File</a>
</body>
</html>

Details : here when you will click on anchor or download button it will take you at download.php fille with the file name hey_baby.mp3.

Step 3:



  • Now create a download.php file as follows . save this file in your xamp folder with download.php

<?php
if(!empty($_GET['file'])){ $Name_Of_File = basename($_GET['file']);
$Path_of_file = 'myfolder/'.$Name_Of_File;
    if(!empty($Name_Of_File) && file_exists($Path_of_file)){
        // Define headers
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$Name_Of_File");
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: binary");
        readfile($Path_of_file);                     //for reading the file
        exit;
    }else{
        echo 'The file you want here does not exist.';
    }
}
?>

Details : 


  •  First of all it will check if the file is empty or not if empty it will display "the file you want does not exist"
  •  Than the basename function will take the base name of file (hey_baby ) in a variable.
  •  Than $path_of_file variable will contain the path of the file make sure you are using correct path.
  •  Now define the headers the headers will play the following roles

cache-control : this header will define wheather the information may be cached in browser or not this may have following values

Public - may be cached in public shared caches.
Private - may only be cached in private cache.
No-Cache - may not be cached.
No-Store - may be cached but not archived.


Content-Disposition : this header is main header for downloading the file it will take the file name for force downloading.

Content-Type : This header normally defined the mime type of a file in this case it can be a zip file or application file.

Content-encoding : This header will define while downloading how the file will be transfered means which type of encoding will be used by browser.

Always remember in a script or in web first of all when we surf protocol sends headers first in the url after that all the things will happen in the url.

5 comments: