• 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 #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
 
 
0 #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
 
 
0 #126 Jan Tomaszewicz 2017-07-08 23:31
I read this article fully on the topic of the resemblance of latest and preceding technologies,
it's remarkable article.
Quote
 
 
0 #125 Jaroslaw Mlodawski 2017-07-07 07:01
Definitely believe that that you stated. Your favorite
reason appeared to be on the web the simplest factor to have in mind of.
I say to you, I definitely get annoyed while folks
think about issues that they plainly do not recognise about.
You controlled to hit the nail upon the highest and
defined out the whole thing with no need side effect , other people could take
a signal. Will probably be back to get more. Thanks
Quote
 
 
0 #124 Lewin Bialy 2017-07-07 04:12
I know this website provides quality depending articles or
reviews and additional information, is there any other website which offers
these kinds of data in quality?
Quote
 
 
0 #123 941wan.Net 2017-07-07 00:46
This piece of writing will assist the internet people for setting up new webpage or even a blog from start to end.



my homepage; 941wan.Net: http://941Wan.net/member/space.php?uid=497197&do=blog&id=466625
Quote
 
 
0 #122 Krystian Matuszczyk 2017-07-06 08:54
I really like your blog.. very nice colors & theme. Did you make this website
yourself or did you hire someone to do it for you?
Plz answer back as I'm looking to construct my own blog and would like to find out where
u got this from. kudos
Quote
 

Add comment

No bad words.


Security code
Refresh


New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1512 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2246 Hits
Inverse kinematics Machine Learning and Robotics
Read More 2738 Hits
gpsim tutorial Machine Learning and Robotics
Read More 3152 Hits

New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1512 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2246 Hits
Inverse kinematics Machine Learning and Robotics
Read More 2738 Hits
gpsim tutorial Machine Learning and Robotics
Read More 3152 Hits
Design by i-cons.ch / etosha-namibia.ch