Page 1 of 1

Help with protected download

Posted: Fri May 16, 2008 2:28 am
by CIMO
Hi guys i'm in trouble with my file list....i'm tryng to make a register user download things in my file list, but here is the problem....if anyone copy the URL plus the name of the file and insert in another page of internet explorer, he is able to download even without the registration on my site...how can i deny the download from outside my site? If anyone knows anything i appreciate your help =)

Re: Help with protected download

Posted: Fri May 16, 2008 2:36 am
by lafever
You can store the files outside of your public_html directory and use something I used on a script before like so:

Code: Select all

 
$dir = "../directory";    // supply a path name.
$name = "installer.msi"; // supply a file name.
$file = $dir.'/'.$name; // combine the path and file
 
     // translate file name property for IE
     if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")){
        $fileName = preg_replace('/\./', '%2e', $name, substr_count($name, '.') - 1);
    }
 
    // make sure the file exists
    if(!$filedl=@fopen($file,'r')){
        die("File cannot be opened for download or does not exist. If you feel this is an error, contact blah");
    // send headers for download
    } else {
        header("Cache-Control: ");                                              // IE needs this blank to work
        header("Pragma: ");                                                     // IE needs this blank to work
        header("Content-type: application/octet-stream");                       // application/octet-stream works for any file type
        header("Content-Disposition: attachment; filename=\"".$fileName."\"");  // display info on download
        header("Content-length:".(string)(filesize($file)));                    // display info on download
        sleep(1);
        fpassthru($filedl);
    }
 
 
Then in your script that checks if the user is logged in you can make a download.php file with an ID number for the files and change up the script a little bit.