• 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 #230 ocean king rig 2018-12-12 09:55
This will be the great idea when they give it to women. They will give an unbiased
opinion about which used trailers for sale are
finest ones that you. Doubt and fear will destroy all but the best laid out plans.
Quote
 
 
0 #229 lucky palace casino 2018-12-12 02:39
Adsense is best suited for when a lot more has a direct focus.
"Content is the king" -this remarks has become saying by people.
SEO is not easy and what makes it doubly hard is the ever changing
pace of things.
Quote
 
 
0 #228 lucky palace casino 2018-12-12 02:38
Adsense is best suited for when a lot more has a direct focus.
"Content is the king" -this remarks has become saying by people.
SEO is not easy and what makes it doubly hard is the ever changing
pace of things.
Quote
 
 
0 #227 scr888 download 2018-12-11 09:27
Nuts high in fats, but these fats are wonderful as very good monosaturated and unsaturated fatty acids and.
That is a person always have to be inside your toes if you are around the trying to make money.
Quote
 
 
0 #226 3win8 apk 2018-12-10 14:34
Thanks in support of sharing such a nice idea, paragraph is pleasant, thats why i have read it fully
Quote
 
 
0 #225 lucky palace casino 2018-12-10 09:13
excellent points altogether, you just received a new reader.
What may you suggest about your submit that you simply made a few days
ago? Any sure?
Quote
 
 
0 #224 mega casino slots 2018-12-08 08:35
Hello there! Do you use Twitter? I'd like to follow you
if that would be ok. I'm undoubtedly enjoying your blog and look forward
to new posts.
Quote
 
 
0 #223 918Kiss Login 2018-12-07 15:35
Very great post. I simply stumbled upon your weblog and wished
to mention that I have really loved surfing around your weblog posts.

In any case I'll be subscribing in your feed and I am hoping you write once more soon!
Quote
 
 
0 #222 live22 casino 2018-12-05 21:59
My family members every time say that I am killing my time here
at net, however I know I am getting experience all the time by reading thes fastidious content.
Quote
 
 
0 #221 3win8 maintenance 2018-12-05 00:34
The search engines just love great content and because people go online to find information and facts they need.
Of course, you must best solution those questions which whining a lot about.
Quote
 
 
0 #220 mega casino slots 2018-12-04 00:42
Duplicate content and boring content for client. Don't underestimate the time
and learning curve with site builders. In this article Let
me give you four simple measures for starting a enterprise.
Quote
 
 
0 #219 ntc33 2018-12-01 21:24
Good day! I could have sworn I've been to your blog before but after
browsing through some of the articles I realized it's new to me.

Anyhow, I'm certainly delighted I stumbled upon it and I'll
be bookmarking it and checking back frequently!
Quote
 
 
0 #218 m rollex11 2018-12-01 19:16
This is your retail store front so people with your niche can find you.
In case the website makes a speciality of one particular product, service or significance.
Advertise on acknowledged websites look engines.
Quote
 
 
0 #217 ace333 apk 2018-11-29 01:25
They may just like your site this much they will also click an Adsense ad to see what else they like.
Their industry is designing website. Important to know the whole novel.
Quote
 
 
0 #216 joker 123 2018-11-27 05:56
On the other hand, creating articles, it's always good guide keep a clean page at reach.
A typical reader doesn't take period to study the whole article but they analyze for key factors of your article.
Quote
 
 
0 #215 sky777 2018-11-26 21:48
Simply wish to say your article is as surprising.
The clarity in your put up is just cool and that i could assume you are knowledgeable in this subject.
Fine along with your permission allow me to snatch
your feed to keep up to date with drawing close post. Thanks 1,000,000 and please keep
up the gratifying work.
Quote
 

Add comment

No bad words.


Security code
Refresh


New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 2024 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2863 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3874 Hits
gpsim tutorial Machine Learning and Robotics
Read More 9367 Hits

Most read content

A* search algorithm Machine Learning and Robotics
Read More 13866 Hits
gpsim tutorial Machine Learning and Robotics
Read More 9367 Hits
PID regulator Machine Learning and Robotics
Read More 8440 Hits

New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 2024 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2863 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3874 Hits
gpsim tutorial Machine Learning and Robotics
Read More 9367 Hits
Design by i-cons.ch / etosha-namibia.ch