I am working on a website project, where you can login.
I want to make a system where you can request a file, but the link to the file should only be valid for a certain amount of time. After the certain amount of time the link should expire and stop working (maybe a show a 404 error).
-I am using php and MySQL database
Thank you! BTW: I am new on this platform
CodePudding user response:
OK I think I found the answer to my question: https://www.tutorialswebsite.com/how-to-create-one-time-temporary-download-link-with-expiration-in-php/
CodePudding user response:
Start with these 2 tables.
Whenever you upload file or manually add it, add an entry in the files table.
Whenever someone request a file, generate a link for it in links table like https://host/download.php?id=the_random_string
which will point to the real file_id, with an expiry date.
files
- id
- name
- physical_path
links
- id
- file_id
- random_string
- expiry
So now you should have a file download.php
(pseudo code)
$str = $_REQUEST['id'];
$sql = "select file_id from links where random_string=$str AND expiry hasn't passed";
$file_id = $row->file_id;
$sql = "select physical_path from files where id = $file_id";
$physical_path = $row->physical_path;
do_download($physical_path);