3

Im making a website for a band, and they are giving out their EP for free, but they want it so the user has to enter their email address before downloading... How would this be done in php?

2 Answers 2

2

The downloadable should be placed out of the reach of web user but within your PHP script reach. Then once user is done filling form, you can then force download the file contents by opening it locally using say "fopen".

Update (Adding Sample Code):

Suppose the file is "txt.txt" which could be in your script reach. You will open it, read and then put the contents after calling header and telling it that its an attachment (force download)

$done  = true;
        if($done == true){

            $filename = "txt.txt";
            $conn = fopen($filename,"r");

            $contents = fread($conn, filesize($filename));
            fclose($conn);
            header('Content-type: text/plain');         
            header('Content-Disposition: attachment; filename="downloaded.txt"');
            echo $contents;



        }
2
  • How do i make a file downloadable using fopen? May 29, 2012 at 13:26
  • I have updated my answer with code. The filename "download.txt" will be file name shown to user.
    – Arfeen
    May 29, 2012 at 13:35
0

If you're storing the emails somehow, then simply set a $_SESSION value when they submit their email, and when writing the page, if the part of the $_SESSION value has been set, then provide a link to the media.

To start a session & set the value:

session_start();
$_SERVER['hasEmail']=true;

And in the page:

session_start();
if ($_SERVER['hasEmail']) {
    //Provide link
}

You could then also have the media link take you to a PHP script which uses fopen() or similar to get the file from another location on your filesystem out of reach of the user, such as one under .htaccess blocking, and it'll only provide the media is the $_SESSION value is set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.