• 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 #136 iphone 7 kilif 2017-11-13 04:52
I used too be able to find good info from your content.

Alsso visit my homepage - iphone 7
kilif: http://Www.Baktit.info/applenin-son-modeli-iphone-7-ve-iphone-7-plus-kirmizi/
Quote
 
 
0 #135 Nolan 2017-11-12 20:55
Having read this I believed it was rather informative.
I appreciate youu spending some time and effort to put this informative
articlpe together. I once again find myself personally spending a lot of time both
reading and postng comments. But so what, it was still worthwhile!
Quote
 
 
0 #134 curly hair 2017-10-05 10:24
Hello! This is kind of off topic but I need some guidance from an established blog.
Is it hard to set up your own blog? I'm not very techincal but I can figure
things out pretty fast. I'm thinking about creating my own but I'm not
sure where to begin. Do you have any points or suggestions?
Thank you
Quote
 
 
0 #133 Site de Casino 2017-09-25 14:12
Alex, teu complicação com certeza é de origem psicológica.
Quote
 
 
0 #132 broderie 2017-09-24 00:26
Wow, awesome blog layout! How long have
you been blogging for? you made blogging look easy. The overall look of your website is fantastic, as
well as the content!
Quote
 
 
0 #131 cboe equity put call 2017-08-19 14:34
you are truly a just right webmaster. The site loading speed is incredible.
It seems that you're doing any distinctive trick.

Furthermore, The contents are masterwork. you have performed a fantastic job in this subject!
Quote
 
 
0 #130 search 2017-08-12 18:24
Well wrіtten articles work as advertising, so yoᥙ get the links onto artiіcle dirеctories.
Bob then goes to hhis or hеr own blog and writes a post of his very own over it, linking back to Alice's original
post. Obviously if somebodү applies nofolⅼow ѡіth a link it's got no search: https://www.fiverr.com/cldisplay_seo/promote-and-market-your-ecommerce-ebay-shopify-etsy-amazon-product-or-blog engine ranking value eventhough
it still has the neеd for having the capacity to pass traffic
through it to yоur websіte.
Quote
 
 
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
 
 
+1 #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
 

Add comment

No bad words.


Security code
Refresh


New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1599 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2361 Hits
Inverse kinematics Machine Learning and Robotics
Read More 2863 Hits
gpsim tutorial Machine Learning and Robotics
Read More 4547 Hits

New articles

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