#native_company# #native_desc#
#native_cta#

How To Download Files Located Outside Web Directory

By PHP Builder Staff
on April 17, 2012

This quickie will show you how to download files from the webserver, even though those files may not be located outside the webserver’s htdocs directory.

First, you will want to create a database in MySQL. I used phpMyAdmin because it is extremely easy to use. Create a user with permissions on that database then create the following table within the new db:

CREATE TABLE files (
   file_id int(11) NOT NULL auto_increment,
   file_path varchar(100) NOT NULL,
   file_name varchar(100) NOT NULL,
   KEY file_id (file_id),
   UNIQUE file_id_2 (file_id)
);

This will hold the path and name of the files available to download.

Now, here are the scripts:

<?
// filename = index.php
// super simple file download script part 1
// Author – Christopher Hall
// this code may be freely distributed

// db parameters, change them to fit your setup
$db_user = “download”;
$db_pass = “download”;
$db_host = “localhost”;
$db_name = “download”;

// connect to mysql db
$conn = mysql_connect($db_host, $db_user, $db_pass);

// setup the query
$query = “select * from files order by file_id”;

// get result
if($result = mysql_db_query($db_name, $query, $conn)) {

    // loop through results
    while($row = mysql_fetch_array($result)) {
        
        // give the user an href so they can get to the file
        echo “<a href=”dl_file.php?fid=”.$row[“file_id”].””>”.$row[“file_name”].”</a>
n”;
    }
}

// close db connection
mysql_close($conn);
?>

And here is the script that does all the work:

<?
// filename = dl_file.php
// super simple file download script part 2
// Author – Christopher Hall
// this code may be freely distributed

// important!!!
// do not put any html. text, blank lines, etc, above your code block
// otherwise, the header() functions will not work, causing the script to choke

// db parameters, change them to fit your setup
$db_user = “download”;
$db_pass = “download”;
$db_host = “localhost”;
$db_name = “download”;

// connect to db
$conn = mysql_connect($db_host, $db_user, $db_pass);

// setup the query
$query = “select file_path, file_name from files where file_id = $fid”;

// get results
if($result = mysql_db_query($db_name, $query, $conn)) {
    
    // if we have results
    if($row = mysql_fetch_array($result)) {

        // get filename and path
        $file_name = $row[“file_name”];
        $file_path = $row[“file_path”];
                
        // send the necessary headers.
        // i found that these work well.
        header(“Content-Type: application/unknown”);
        header(“Content-Disposition: filename=$file_name”);

        // open the file for reading and start dumping it to the browser
        if($fp = fopen($file_path.$file_name, “r”)) {
            while(!feof($fp)) {
                echo  fgets($fp, 4096);
            }
            // close the file
            fclose($fp);
        }
    }
}

// close db connection
mysql_close($conn);
?>

And that is all there is to it. Have fun with it!