The code you got from will only check if
user comes from a specific place and
then do stuff depending on that!
And, the files you want to protect should be
outside of your http doc root!
You have to implement your self functions for
reading and serving the files to the visitors.
You can use "fread" to read the file into a buffer
and then output it to the visitor with the
appropriate headers.
Look at the following code(2 scripts) for a quick example!
You have to change the appropriate variable to reflect your needs!
These are as said quick and dirty examples that can be
developed further, but I leave this to you!
filename: getFile.php
<?php
// The page you want your visitors to come from
#$my_referer_page = 'http://www.server.com/files/getFile.php';
// The page you want your visitors to go to in case that they do not come from the above page!
// This can be the same as above page if you don't want to show any error message!
#$my_error_page = 'http://www.server.com/files/error.php';
// directory where the files are stored. This should be outside of your http doc root!
$files_dir = '/dir/to/my/files/';
$file_name = $GET["file_name"];
if ($SERVER["HTTP_REFERER"] != $my_referer_page)
{
header ("location: $my_error_page");
}
else if (empty($file_name))
{
echo "No file selected for download! <br>";
echo "Please go <A HREF=\"$my_referer_page\">back</A> and and select one!<br>";
}
else
{
$file_path = $files_dir.$file_name;
$fp = fopen ($file_path, "r");
$file_buffer = fread ($fp, filesize ($file_path));
fclose ($fp);
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=$file_name");
echo $file_buffer;
}
?>
filename: file_index.php
<?php
// directory where the files are stored. This should be outside of your http doc root!
$files_dir = 'C:/';
// name of the script that will handle the downloads
$downloadManager = 'getFile.php';
echo "<b>Files available for download: </b><br>\n";
$handle = opendir("$files_dir");
while (false !== ($file_name = readdir($handle))) {
if ($file_name != "." && $file_name != "..") {
echo "<A HREF=\"$downloadManager?file_name=$file_name\">$file_name</A><BR>\n";
}
}
closedir($handle);
?>