First you put all the files in the directory bellow and after you have to chmod the directory to disable the web and other kind of access. (chmod 200).
And then what you can do is something like:
$dir_path = "/your/path/to/directory";
if($access == "ok") {
chmod($dir_path, 0755);
//Rest of the code to download the file
} else {
print "Access Denied";
}