• 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 #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
 
 
0 #182 99Loreen 2018-07-31 06:52
I can see that your website probably doesn't have much visits.
Your posts are interesting, you only need more new visitors.
I know a method that can cause a viral effect on your
blog. Search in google: dracko's tricks make your content go viral
Quote
 
 
0 #181 Maxman 2018-07-29 20:02
I every time spent my half an hour to read this webpage's
articles or reviews daily along with a cup of coffee.
Quote
 
 
0 #180 Member XXL Conținut 2018-07-16 06:34
I’m not that much of a internet reader to be honest but your blogs really nice, keep it up!
I'll go ahead and bookmark your website to come back later.
Cheers
Quote
 
 
0 #179 marimea penisului 2018-07-14 21:27
This is a topic that's close to my heart... Cheers! Where are your contact details though?
Quote
 
 
0 #178 remy hair extensions 2018-07-10 19:48
Hello! I just wanted to ask if you ever have any issues with hackers?
My last blog (wordpress) was hacked and I ended up losing a few months of
hard work due to no data backup. Do you have any methods to protect against hackers?
Quote
 
 
0 #177 www.lacewigsbuy.com 2018-06-29 00:04
Hi Dear, are you truly visiting this web site regularly, if
so then you will definitely take fastidious
know-how.
Quote
 

Add comment

No bad words.


Security code
Refresh


New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1940 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2756 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3580 Hits
gpsim tutorial Machine Learning and Robotics
Read More 8687 Hits

Most read content

A* search algorithm Machine Learning and Robotics
Read More 10066 Hits
gpsim tutorial Machine Learning and Robotics
Read More 8687 Hits

New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1940 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2756 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3580 Hits
gpsim tutorial Machine Learning and Robotics
Read More 8687 Hits
Design by i-cons.ch / etosha-namibia.ch