Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Dynamic Download Link in PHP

Tags:

php

email

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.

like image 881
user434509 Avatar asked Feb 22 '23 03:02

user434509


1 Answers

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!

like image 64
gahooa Avatar answered Mar 01 '23 13:03

gahooa