Allow Protected File Downloads

Discussion in 'PHP' started by Ascendancy, Apr 22, 2008.

  1. #1
    I want to know how to have a link for the user to download a file, and have that be the only way to download it. Let's say we have somesite.com/files/file.zip, and we want to allow the user to download that only after they have recieved an e-mail containing a link that allows them to download it (so it could be somesite.com/download?id=12133&user=theperson). However, if a person went in the URL right to the .zip file, it wouldn't let them download it so it could be protected from downloading right away.

    How would I go about doing something like this? Thanks in advance for any help.
     
    Ascendancy, Apr 22, 2008 IP
  2. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #2
    Here's what I would do:
    Upload the files to a particular directory, and restrict direct access (or directory listing).

    Make the file names have a completely random name that is generated by PHP upon upload.

    Whenever the user is sent an email, have a special key sent along with them. So their URL would be:
    http://www.site.com/file.php?key=a9837vna4x20

    or something like that. In your database, for the row that matches that key have the file they would like to download (the filename). You will then need an individual file, that once accessed grabs the file (using content-type) and force downloads the file. Before giving the download, however, the database is checked to see if this file was downloaded already by this key. If it was, kill the attempt . . . otherwise, let the download continue.

    That should do the trick for you. Let me know if that made any sense :p

    Cheers,
    Louis
     
    Louis11, Apr 23, 2008 IP
  3. Ascendancy

    Ascendancy Active Member

    Messages:
    1,721
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Thanks Louis, that did clear a lot up, and your idea makes a lot of sense. I just have a quick question that I'm not sure if you could help me with, but I am just not sure how to use the "content-type" keyword(if it's a keyword). Could you give an example of a block of code using it?
     
    Ascendancy, Apr 24, 2008 IP
  4. andrew1056

    andrew1056 Peon

    Messages:
    196
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The content type is a php header... it would look like this:

    
    // We'll be outputting a PDF
    header('Content-type: application/pdf');
    
    // It will be called downloaded.pdf
    header('Content-Disposition: attachment; filename="downloaded.pdf"');
    
    // The PDF source is in original.pdf
    readfile('original.pdf');
    
    Code (markup):
    You have to define the content type basically so the browser knows what to do with it. I haven't worked with files in PHP in awhile... so PHP may be able to define the content type automatically ??
     
    andrew1056, Apr 24, 2008 IP
  5. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #5
    I haven't done it in a while either, but to the best of my recollection you have to define the content type on your own, otherwise it will just spit out the data for the file.

    http://www.php.net/header
     
    Louis11, Apr 24, 2008 IP
  6. Ascendancy

    Ascendancy Active Member

    Messages:
    1,721
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    90
    #6
    Oh okay, that makes a lot more sense. Thanks for the help guys!
     
    Ascendancy, Apr 24, 2008 IP