How to make temporary link for a downloadable file
Link to something like /downloads/abb76b3acbd954a05bea357144d614b4
, where abb... is a random string such as a salted hash of the current time. When you make this link for someone, store the random string in a database table. This table might look like:
+----+-------------+----------------------------------+---------------------+---------------------+
| id | filename | token | created | modified |
+----+-------------+----------------------------------+---------------------+---------------------+
| 1 | image.jpg | abb76b3acbd954a05bea357144d614b4 | 2012-03-26 19:36:41 | 2012-03-26 19:36:41 |
| 2 | program.exe | 19660d0f5e0ca3d42e1718de5d0a1d7e | 2012-08-29 11:07:58 | 2012-08-29 11:07:58 |
+----+-------------+----------------------------------+---------------------+---------------------+
When you get a request at /downloads/...
, look up the random string and send back the correct file. If the created
timestamp is too old, don't do so. Use a cronjob to clean out old table rows, or less ideally, do this every time someone makes a request to /downloads/...
.
Use some code like this
$path="uploads/";
$actualfilename=$path.$filename;
$fakefilename="downloadfile.pdf";
if($typeofview=="download") {
@readfile($actualfilename);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . $fakefilename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($actualfilename));
header('Accept-Ranges: bytes');
exit;
add this to your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule fakefile.php(.*)$ orignalfile.php?$1 [L,QSA]