Help with protected download

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CIMO
Forum Newbie
Posts: 4
Joined: Wed May 07, 2008 4:54 am

Help with protected download

Post 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 =)
User avatar
lafever
Forum Commoner
Posts: 99
Joined: Sat Apr 05, 2008 2:03 pm
Location: Taylor, MI

Re: Help with protected download

Post 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.
Post Reply