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.
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 Cheers, Louis
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?
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 ??
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