简体   繁体   中英

How to create Dynamic Download Link in PHP

Is there a way we can create a dynamic download link in PHP for a single file for some period of time or the download link expires after that time. After that period the download link changes.

Actually I have a requirement where the download link should be accessible only through a particular email. I can't add that file as an attachment because of its size.

Can any one help me in this.

One solution:

  1. Create a Database table which stores a large unique ID (random), and the name/location/content of the file to download. Also include an expire date.

     id | filename | expires ----------------------+--------------------+-------------------- fsdhfs7dfsniuf92un3f2 | secret.doc | 2012-03-23 23:32:32 sdf8shdf829nf32ufn23f | secret2.doc | 2012-03-13 23:32:33
  2. Email a link to your end user... The link should be something like:

     http://yoursie.com/download/fsdhfs7dfsniuf92un3f2
  3. Use an apache rewrite rule (mod_rewrite) which will capture the nice looking link and pass it to a PHP page:

     RewriteEngine on RewriteRule ^/download/([a-z0-9]{20})$ /download.php?id=$1
  4. In that script, download.php , look at $_GET['id'] . Run a database query to look up the record. Check the expiration date. If all is OK, then proceed.

  5. Either use the PHP script to output the correct headers and download the file, or send an internal redirect to a front-end proxy like nginx, which will offload the download process to nginx and not tie up PHP with the download.

Either way, you have a secure, expireable link that you can send to your end users.

Take care!

Sure, store the path to the file in a database, along with a unique identifier and a timestamp of expiry. Then when a user accesses a link with the unique ID in the get variables, check the timestamp and fetch the file/display an error accordingly.

Database table:

id file (out of publicly accessible path) expires

Then have a script get-file.php?id=XXXXXX

<?not-real-code
Delete everything expired from table

Look up record for id=XXXXXXX

If exists then use something like readfile() to read and output file

Delete record in table

you could install a database where you put your file, a timestamp and hits in a table. In your script you could then check for the timestamp + 86400*day (with 86400 being the amount of seconds a day). If the download is in that timeframe, it will be allowed, otherwise not. You could then output the file to be downloaded using readfile() and adjust the encoding using header() calls. For a more specific help you would have to post some code.

  1. Create a folder on the web server with an index.php file in it which says that the file is no longer available.
  2. Create a .htaccess file pointing at the index.php if needed.
  3. Add the file you want people to download to the folder on the web server.
  4. Send out the email with the link.
  5. When you feel it's time has run out delete the file and people will go to index.php instead.

Note: If you need to automate any of this look into cron jobs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM