• 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 #196 Marguerite 2018-09-22 09:01
1.845). LASCH, Christopher. 1 p. 2, 2003. E também
vice-versa.
Quote
 
 
0 #195 iworking.vn 2018-09-13 09:09
I like this site іt's a master piece! Glad Ι discovered tһіs
on google.
Quote
 
 
0 #194 ArleneJuicy 2018-09-07 11:19
Hello. I see that you don't update your blog too often.
I know that writing articles is boring and time consuming.

But did you know that there is a tool that allows you to create
new posts using existing content (from article directories or other pages from your niche)?
And it does it very well. The new articles are high quality and pass the copyscape test.
Search in google and try: miftolo's tools
Quote
 
 
0 #193 카지노사이트 2018-08-25 16:25
Euro - Millions lottery email address details are also
displayed immediately after the draw held, you may also view result videos.
Ideally, payout percentages for online casinos ought to be
above 98 percent, and practically all the major ones are.
Chance is needless to say, a game title of randomness and also deceitful order, but, in the end, the outcome which a random set of numbers can generate
will usually consume a pair of rules.
Quote
 
 
0 #192 phonographic.science 2018-08-18 21:39
Hello There. I found your blog using msn. This is a very well written article.

I'll be sure to bookmark it and come back to read more of your useful information. Thanks for the
post. I will certainly comeback.
Quote
 
 
0 #191 Jeanna 2018-08-17 23:15
Hey I am so grateful I found your web site, I really found you by mistake, while I
was searching on Digg for something else, Regardless I am here now and would just like to say thanks for a marvelous post and a all round entertaining blog (I also
love the theme/design), I don't have time to go through
it all at the minute but I have bookmarked it and also
added in your RSS feeds, so when I have time I will be back to read a great deal more,
Please do keep up the superb job.
Quote
 
 
0 #190 http://xurl.es/2r7f7 2018-08-17 16:04
This post will assist the internet people for creating new
weblog or even a weblog from start to end.
Quote
 
 
0 #189 Stone Island 2018-08-12 21:45
Hello, i read your blog occasionally and i own a similar one and i was just
curious if you get a lot of spam remarks? If so how do you
protect against it, any plugin or anything you can suggest?
I get so much lately it's driving me mad so any support is very much appreciated.
Quote
 
 
0 #188 hogan uomo 2018-08-08 09:55
I know this if off topic but I'm looking into starting my
own weblog and was wondering what all is needed to get
setup? I'm assuming having a blog like yours would cost a
pretty penny? I'm not very internet smart so I'm not 100% positive.
Any recommendations or advice would be greatly appreciated.
Many thanks
Quote
 
 
0 #187 ferragamo slippers 2018-08-08 06:37
Hey! I know this is somewhat off topic but I was wondering
which blog platform are you using for this website?
I'm getting tired of Wordpress because I've had issues with hackers and I'm looking at options for
another platform. I would be great if you could point me in the
direction of a good platform.
Quote
 
 
0 #186 Emmanuel 2018-08-08 05:51
Greetings! This is my 1st comment here so I just wanted to give a quick shout out and tell you I truly
enjoy reading your articles. Can you suggest any other blogs/websites/forums that go over the same topics?
Many thanks!
Quote
 
 
0 #185 George 2018-08-08 00:47
Admiring the time and energy you put into your website and
detailed information you present. It's nice
to come across a blog every once in a while that isn't the same out of
date rehashed information. Wonderful read! I've saved your site and I'm including your RSS feeds to my Google account.
Quote
 
 
0 #184 stone island shorts 2018-08-01 23:10
Hmm it seems like your website ate my first comment (it was
super long) so I guess I'll just sum it up what I wrote and say, I'm thoroughly enjoying
your blog. I as well am an aspiring blog writer but I'm still new to
everything. Do you have any suggestions for inexperienced
blog writers? I'd certainly appreciate it.
Quote
 
 
0 #183 jocuri.zona.ro 2018-07-31 20:02
Great post! We will be linking to this great
content on our site. Keep up the good writing.
Quote
 

Add comment

No bad words.


Security code
Refresh


New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1962 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2780 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3651 Hits
gpsim tutorial Machine Learning and Robotics
Read More 8875 Hits

Most read content

A* search algorithm Machine Learning and Robotics
Read More 11225 Hits
gpsim tutorial Machine Learning and Robotics
Read More 8875 Hits

New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1962 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2780 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3651 Hits
gpsim tutorial Machine Learning and Robotics
Read More 8875 Hits
Design by i-cons.ch / etosha-namibia.ch