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:
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
Email a link to your end user... The link should be something like:
http://yoursie.com/download/fsdhfs7dfsniuf92un3f2
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
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With