Download file script in PHP

Some times we  need to create a download link of any file in our site. We can point a direct link of the file to download but in this case we can get the absolute path of that file which is not preferred to us. Moreover some browsers redirect the download link path in a new window.

Hence we can create a separate PHP script through which we can download any file and there will be no possibility to access the absolute path of the file and no redirection to the other page. We can get the save file pop window in the same window.
Step 1: Create a PHP file named download.php and put the following codes in it.

Step 2: Now wherever you want to put the download file link, put the following code:

 <a href="download.php?filename=yourfilename.zip">Click here to Download the File</a>

Posted

in

by

Tags:

Comments

5 responses to “Download file script in PHP”

  1. EKG Interpretation avatar

    Thank you for publishing this for free. It works like a charm, and I am no programmer.

  2. nahid avatar

    this works fine!!!!!
    very simple

  3. Will Knot B. Revealed Snr. avatar
    Will Knot B. Revealed Snr.

    :/ Needs updating for PHP 5

    “Function eregi() is deprecated”

  4. Sanjay Bhowmick avatar

    Use “preg_match” function instead. Check if this works or not.
    if (preg_match(“..”,$filename)) die(“I’m sorry, you may not download that file.”);

    1. Will Knot B. Revealed Snr. avatar
      Will Knot B. Revealed Snr.

      That works perfectly now. Thank you Sanjay 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *