First... If you truly want to protect the file, you need to move it out of a public web directory.
Second... Whitelisting IP's is very easy.
Here is a rough example.
<?php
class Files
{
function __construct()
{
include 'db.php'; //start your database connection
$this->mydir = "/var/protectedFiles/"; //note how it is not in the web directory
$this->ip = $_SERVER['REMOTE_HOST']; //gets the IP address of the user
//build an array of the url, then pickout the file they are looking for
$request = parse_url($_SERVER['HTTP_REFERER']);
$path = $request['path'];
$path_parts = explode('/', $path); //seperate the path string into an array
$this->myfile = $path_parts[1]; //if the url is like so... http://example.com/files/myfile.zip
//run through the authorization process and give them what they deserve!
$auth = $this->check_IP();
if($auth === "TRUE")
{
$file_exists = $this->check_file();
if($file_exists === "TRUE")
{
$file = file_get_contents($this->mydir.$this->myfile);
//set an optional header
header('HTTP/1.1 200 OK');
header('Content-Type: application/zip');
//print the file!
print_r($file);
}
else
{
echo "We could not find the file you are looking for!";
die;
}
}
else
{
echo "Access Denied";
die;
}
}
private function check_IP()
{
//check your IP database for an IP
$sql = "SELECT * FROM `ip_whitelist` WHERE `ip` = '".$this->ip."'";
$query = mysql_query($sql);
$valid = mysql_num_rows($query);
if($valid === 1)
{
return "TRUE"; //if the IP exists in your database
}
else
{
return "FALSE"; // if the IP does not exist in your database
}
}
private function check_file()
{
if(file_exists($this->mydir.$this->myfile))
{
return "TRUE"; // I found a file!!!
}
else
{
return "FALSE"; //I could not find what you are looking for!
}
}
}