• Increase font size
  • Default font size
  • Decrease font size

dynamic download link

Once, I had to face problem, how to generate download link dynamicly. I did not want everybody could download my files by copy pasting link into browser. Here is the solution I have created:

First we need a regular download link. This link is directed to the php script which handles whole process of downloading. Two variables are send to the php script by "GET".

<?php
//uses hash of date to ensure security
$password = "pass";
$strToHash = date('m.d.y').$password;
$hash = hash('md5', $strToHash);
echo "<a href='dl.php?filename=icon.ico&id={$hash}'>working DOWNLOAD</a><br><br>";
echo "<a href='dl.php?filename=icon.ico&id=1'>not working DOWNLOAD</a>";
?>

  1. filename which we want to download
  2. id which is our MD5 hash generated based on the date and a password.
Everythig looks like this:

As you can see, hypertext runs dl.php script. This script has two tasks:

  1. control if it was run by our link. It is done by the hash control
  2. allow us to download the requested file (defined by filename variable)
Here is the code:
<?php
$download_path = "C:\\";    //directory where downloadable files are stored
$password = "pass";         //password
$filename = $_GET['filename'];
$hash1 = $_GET['id'];
$strToHash = date('m.d.y').$password;
$hash2 = hash('md5', $strToHash);
if(strcmp($hash1, $hash2) != 0){
die("Please try again later.");
}
// Detect missing filename
if(!$filename) die("I'm sorry, you must specify a file name to download.");
// Make sure we can't download files above the current directory location.
if(eregi("\.\.", $filename)) die("I'm sorry, you may not download that file.");
$file = str_replace("..", "", $filename);
// Make sure we can't download .ht control files.
if(eregi("\.ht.+", $filename)) die("I'm sorry, you may not download that file.");
// Combine the download path and the filename to create the full path to the file.
$file = "$download_path$file";
// Test to ensure that the file exists.
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
// Extract the type of file which will be sent to the browser as a header
$type = filetype($file);
// Send file headers
header("Content-type: $type");
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
// Send the file contents.
set_time_limit(0);
readfile($file);
?>

Source code, you may download here.

 

 

Comments  

 
0 #81 Adrian Karbowski 2017-05-22 11:54
Hey there! I realize this is somewhat off-topic but I had to ask.

Does operating a well-established blog such as yours take a large amount of
work? I am completely new to writing a blog however I do write in my journal on a
daily basis. I'd like to start a blog so I will be able to
share my experience and views online. Please let
me know if you have any kind of ideas or tips for new
aspiring bloggers. Thankyou!
Quote
 
 
0 #80 tinyurl.com 2017-05-22 11:11
Após explorando um número da artigos na ѕeu local , еu verdadeiramente apreciar
ѕeu formɑ de blogging . Еu livro marcado ρara meu marcador local lista е
será verificar volta logo . Confira mеu web site também ᥱ diga sua opiniãօ.
Quote
 
 
0 #79 twitter.com 2017-05-19 02:27
I hɑve llearn several gokod stuff herе. Certainly worth bookmarking for revisiting.

I wondᥱr how sso much attempt yyou put tto make one of these wonderful informative website.
Quote
 
 
0 #78 buy eso gold 2017-05-16 14:53
First ooff I ԝant to say greatt bⅼog! I had a quiϲk questiоn in which I'd
like to аsk if you dօn't mind. I was interested to know how youu enter yourself and clear your head before writing.I've had trouble cleɑring my mind in getting my ideas
out. I do take pleasure in writing however it juust seemѕ like
the fiгst 10 to 15 minutes aгe lost simply just tryying tto figuure out how
to begin. Any recommendations oor hintѕ? Thanks!
Quote
 
 
0 #77 Xawery Zdanowski 2017-05-15 13:27
Usually I do not learn post on blogs, but I wish to
say that this write-up very compelled me to check out and do it!
Your writing style has been amazed me. Thanks, quite great post.
Quote
 
 
0 #76 buy cheap eso gold 2017-05-15 08:59
Ꭼverythіng is very open with a veгy clear explanation of the issues.
It was really informative. Your site is extremеly helpful.
Thanks forr shaгing!
Quote
 
 
0 #75 fifa 18 2017-05-14 18:19
I enjoy what you guyѕ tend to be սp too. Succh clevеr
worқ and cоverage! Keep up the fantasticc works gguys I've incorporated you guys to blogroll.
Quote
 
 
0 #74 runescape works 2017-05-14 14:35
Actuаlly ѡhen someone doesn't know afterward its up to othheг visitors that they wіlll help, so here іt occurs.
Quote
 
 
0 #73 Romuald Schmidt 2017-05-13 06:57
What's up, after reading this remarkable piece of writing i am too happy to share my
experience here with mates.
Quote
 
 
0 #72 Tilly 2017-05-12 14:00
Thɑnk ʏoou for another informative blog.Where elsе mmaү just I
get thzt type of information written in such an ideаl way?
I haᴠe a undertaking that I'm simply now working on, annd I have been on the look out for such
іnformatiⲟn.
Quote
 

Add comment

No bad words.


Security code
Refresh


New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1327 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2042 Hits
Inverse kinematics Machine Learning and Robotics
Read More 2487 Hits
gpsim tutorial Machine Learning and Robotics
Read More 590 Hits

New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1327 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2042 Hits
Inverse kinematics Machine Learning and Robotics
Read More 2487 Hits
gpsim tutorial Machine Learning and Robotics
Read More 590 Hits
Design by i-cons.ch / etosha-namibia.ch