SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru james_littler's Avatar
    Join Date
    May 2007
    Location
    Portsmouth, UK
    Posts
    775
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Collect information prior to download

    Hi, I was wondering if there was a way to do the following

    User clicks download file link - > user is asked to enter their name and email - > user clicks ok - > file is downloaded.

    I need this to work in multiple places so the link to the download/file needs to be dynamic (if that's the right work)

    Any advice would be really appreciated
    Thanks
    James

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    You link to a php script (download.php for example) with a download id in the querystring (download.php?id=672347623).
    That php script asks for name and email, validates everything and if all is ok, redirects to the file to download.

    Never coded something like this myself though, so if you need help with the part where it actually starts the download, I can't help you

  3. #3
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by james_littler View Post
    Hi, I was wondering if there was a way to do the following

    User clicks download file link - > user is asked to enter their name and email - > user clicks ok - > file is downloaded.

    I need this to work in multiple places so the link to the download/file needs to be dynamic (if that's the right work)

    Any advice would be really appreciated
    Thanks
    James
    Yes it is possible. There are many ways, but the safest and most cross browser compatible is what guido2004 suggests. You can also use CSS to hide a field. Make the download button trigger an event so that field appears -> user enters email -> clicks ok and php script checks for validity -> download starts. You can also use a javascript prompt box. There are many ways to be creative.

    For the php file you will want to look into the header() function
    Content-type: and Content-Disposition:

    Hope this helps.

  4. #4
    SitePoint Guru james_littler's Avatar
    Join Date
    May 2007
    Location
    Portsmouth, UK
    Posts
    775
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thanks for the replies, I can see how this works now.

    I don't suppose anyone has any examples? It's just I'm pretty new to php and don't know where to start.

    Thanks again
    James

  5. #5
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by james_littler View Post
    Ok thanks for the replies, I can see how this works now.

    I don't suppose anyone has any examples? It's just I'm pretty new to php and don't know where to start.

    Thanks again
    James
    James here is a quick little script I wiped up. It will need editing but should get you going

    PHP Code:
    //You may want to seperate these into files


    function getfile($fileid=""){
        
    /*
            put your database code in here to get the file information. file, extension etc.
            return blank if fileid not found or if email is invalid
            I'll be using a static function for this example
        */
        
        
    return "foo.jpg";
    }

    function 
    checkemail($email=""){
        
    /*
            Put email check code here.
        */
        
    if(preg_match('/[\.a-z0-9_-]+@([a-z0-9_-]+\.[a-z0-9_-]{2,4}|[a-z0-9_-]+\.[a-z0-9_-]{2,4}\.[a-z0-9_-]{2,4})+$/',$email)) return TRUE;
    }


    $theid=is_numeric($idd=$_REQUEST[id])?$idd:"";
    switch(
    $theid){
    default:
        print <<<ECHO
        <form method=GET><a href=?fileid=23&id=1>Download</a><br><a href=?fileid=23dd&id=1>Download (wont work)</a></form>
    ECHO;
    break;
    case 
    1:
        
    $fileid=is_numeric($fild=$_GET[fileid])?$fild:"";
        print <<<ECHO
        <form method=POST><input type=hidden name=fileid value=$fileid>
        <input type=hidden name=id value=2>
        <input type=text name=email value='Enter Email'"> <input type=submit value=OK></form>
    ECHO;
    exit;
    break;
    case 
    2:
    $fileid=is_numeric($fild=$_POST[fileid])?$fild:"";
    $useremail=($usreml=trim($_POST[email]))?$usreml:"";

    $errormsg="Could not download at this time";

    if(
    $fileid && checkemail($useremail)){
    $filename=getfile($fileid);
    $folder='images/';
    $contents="";

    if(
    file_exists($filetoget="$folder$filename")){
        
    //get file contents    
        
    $contents=file_get_contents($filetoget);
            
        
    header("Content-type: application/octet-stream");
        
    header("Content-Disposition: attachment; filename=\"$filename\"");
        
        print 
    $contents;
    }else{
        
    //error message
        
    print $errormsg;
    }}else{
        print 
    $errormsg;
    }
    break;

    Let me know if this helped.

  6. #6
    SitePoint Guru james_littler's Avatar
    Join Date
    May 2007
    Location
    Portsmouth, UK
    Posts
    775
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's great mate, thanks.

    I'll set that up on Monday and will let you know how it goes.

    Cheers

  7. #7
    SitePoint Zealot sdavis2702's Avatar
    Join Date
    Sep 2007
    Location
    Austin, TX
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome... I am going to work with that script as well because I need the exact same thing.
    I build WordPress with the Volatyl Framework.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •