• 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 #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
 
 
0 #176 Marcella 2018-06-24 01:05
This site truly has all of the information and facts I wanted about this subject and didn't know who to ask.
Quote
 
 
0 #175 simhungtran.vn 2018-06-10 05:22
Usuallу І do not read post on blogs, however I wiѕh to saү that this write-up very pressured me to check оut and ⅾⲟ it!

Үour writing style hɑs been surprised mе.
Ƭhanks, vеry nice post.
Quote
 
 
0 #174 Manual 2018-06-10 01:22
I've recently started a web site, the information you
offer on this site has helped me tremendously.
Thanks for all of your time & work.
Quote
 
 
0 #173 connect.inyokern.net 2018-06-03 14:35
Fato é que Turbo Maca Peruvian funciona de verdade!
Quote
 
 
0 #172 terza maglia real 2018-05-28 00:55
I'd like to find out more? I'd care to find out more details.



terza maglia real: http://www.cartoulesletterpress.com/category/computers-internet/
Quote
 
 
0 #171 Grow Faster 2018-05-21 17:07
It's amazing designed for me to have a web page, which is helpful
designed for my know-how. thanks admin

my web blog :: Grow Faster: https://bit.ly/2Is2FRs
Quote
 
 
0 #170 grow Faster 2018-05-21 13:19
I am really impressed with your writing talents as well as with the layout in your
weblog. Is this a paid subject or did you customize it yourself?
Anyway keep up the excellent high quality writing, it is rare
to peer a nice blog like thios one nowadays..

Also visit my page grow Faster: https://bit.ly/2Is2FRs
Quote
 
 
0 #169 Eyelashes ticker 2018-05-21 10:53
I simply couldn't leave your website prior to suggesting tthat I actually enjoyed the standard information an individual provide to your guests?
Is going to be again incessantly in order to inspect new posts

my site Eyelashes ticker: https://bit.ly/2Is2FRs
Quote
 

Add comment

No bad words.


Security code
Refresh


New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1920 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2735 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3467 Hits
gpsim tutorial Machine Learning and Robotics
Read More 8562 Hits

Most read content

New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1920 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2735 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3467 Hits
gpsim tutorial Machine Learning and Robotics
Read More 8562 Hits
Design by i-cons.ch / etosha-namibia.ch