• 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 #129 Outdoor Clothing 2017-07-21 12:14
No matter if some one searches for his vital
thing, so he/she wants to be available that in detail, thus that thing is maintained over
here.

My weblog :: Outdoor Clothing: http://www.monacoarredamenti.com/?option=com_k2&view=itemlist&task=user&id=1134277
Quote
 
 
0 #128 cme pork bellies 2017-07-19 06:54
What's Happening i am new to this, I stumbled
upon this I have found It positively helpful and it has aided
me out loads. I'm hoping to contribute & assist other users like its helped me.
Good job.
Quote
 
 
0 #127 how is oil priced 2017-07-14 15:12
I am really loving the theme/design of your weblog. Do you
ever run into any web browser compatibility problems? A number of my blog audience have complained about my website
not working correctly in Explorer but looks great in Firefox.

Do you have any ideas to help fix this issue?
Quote
 
 
0 #126 Jan Tomaszewicz 2017-07-08 23:31
I read this article fully on the topic of the resemblance of latest and preceding technologies,
it's remarkable article.
Quote
 
 
0 #125 Jaroslaw Mlodawski 2017-07-07 07:01
Definitely believe that that you stated. Your favorite
reason appeared to be on the web the simplest factor to have in mind of.
I say to you, I definitely get annoyed while folks
think about issues that they plainly do not recognise about.
You controlled to hit the nail upon the highest and
defined out the whole thing with no need side effect , other people could take
a signal. Will probably be back to get more. Thanks
Quote
 
 
0 #124 Lewin Bialy 2017-07-07 04:12
I know this website provides quality depending articles or
reviews and additional information, is there any other website which offers
these kinds of data in quality?
Quote
 
 
0 #123 941wan.Net 2017-07-07 00:46
This piece of writing will assist the internet people for setting up new webpage or even a blog from start to end.



my homepage; 941wan.Net: http://941Wan.net/member/space.php?uid=497197&do=blog&id=466625
Quote
 
 
0 #122 Krystian Matuszczyk 2017-07-06 08:54
I really like your blog.. very nice colors & theme. Did you make this website
yourself or did you hire someone to do it for you?
Plz answer back as I'm looking to construct my own blog and would like to find out where
u got this from. kudos
Quote
 
 
0 #121 Anemonalove 2017-07-06 05:14
Hi fellas! Who wants to chat with me? I'm live at HotBabesCams.com, we can chat, you can watch me live for
free, my nickname is Anemonalove , here is my pic:

https://3.bp.blogspot.com/-u5pGYuGNsSo/WVixiO8RBUI/AAAAAAAAAFA/JWa2LHHFI2AkHParQa3fwwHhVijolmq8QCLcBGAs/s1600/hottest%2Bwebcam%2Bgirl%2B-%2BAnemonalove.jpg
Quote
 
 
0 #120 Max Zimna 2017-07-06 04:18
I'm truly enjoying the design and layout of your blog.

It's a very easy on the eyes which makes it much more pleasant for me to come here and
visit more often. Did you hire out a designer to create your theme?

Outstanding work!
Quote
 

Add comment

No bad words.


Security code
Refresh


New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1415 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2115 Hits
Inverse kinematics Machine Learning and Robotics
Read More 2599 Hits
gpsim tutorial Machine Learning and Robotics
Read More 1600 Hits

New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1415 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2115 Hits
Inverse kinematics Machine Learning and Robotics
Read More 2599 Hits
gpsim tutorial Machine Learning and Robotics
Read More 1600 Hits
Design by i-cons.ch / etosha-namibia.ch