Simple PHP File Download Script

18 Dec 2015

So I recently added a download.php script to my website so that I could force downloads of files instead of having users access them through an indexed directory or through their browser.

I found various scripts online but none of them were as clean as I’d have liked them to be so I wrote my own simple script after a bit of research.

In my setup, the download.php file sits at the root of my website and the filevault folder sites one level higher on the web server. This setup ensures users cannot hotlink to files or directly access them, the script must be used. A benefit of this is that you can add restrictions like allowing a file to be accessed by people from a particular country or by those who have a certain cookie set. If you do not have access to the directory above your websites root directory then you are forced into putting your filevault at the websites root directory.

This is the simple PHP File download script:


The following link would force the download of that_file.txt



You can also download the above script (using the script!):
Download download.php Script


Posted in Programming


Leave a Reply


  1. Santhosh veer

    February 6, 2017 at 3:50 PM

    Hey thanks for this code
    I found little error on this code

    if ($_GET[‘file’] != “”) { on line no 14

    Replace it with if (isset($_GET[‘file’])) {

    and in $path_to_filevault_dir = “../filevault”; remove that two “..” full stops

    Thanks for this awesome script 🙂

    • Mohammad Ghasembeigi

      April 25, 2017 at 8:41 PM

      Good call about the $_GET check, i’ve changed the script to instead use (!empty($_GET[‘file’])) which will check if isset as well as if its an empty string.

      However, the $path_to_filevault_dir is not an error. The file vault folder is meant to be located in a non accessible folder on the machine so users can’t directly download the file by avoiding the script. However, you could simply place it somewhere else if you really want.

  2. Mohammad Ghasembeigi

    June 25, 2017 at 9:10 AM

    Cleaned up the above code a little to fix an issue with downloading files with a space in their name.

  3. raj

    October 11, 2017 at 4:55 PM

    perfect script but not show in my website