• 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 #147 Seomarketing101 2018-04-24 01:48
I just could not depart your web site before suggesting that I actually enjoyed the standard info an individual supply on your visitors?
Is going to be again steadily in order to check out
new posts
Quote
 
 
0 #146 Doaa Hosny 2018-04-14 00:19
Excellent, what a webpage it is!This website provides valuable facts to us,
keep it up.

Have a look at my web page: Doaa Hosny: https://swrbook.blogspot.com/
Quote
 
 
0 #145 Manar Ahmed 2018-04-13 22:44
This is my first time go to see at here and i am truly happy to read everthing at alone place.


Also visit my webpage: Manar
Ahmed: https://manarahmed.blogspot.com
Quote
 
 
0 #144 Amir Ali 2018-04-12 19:03
This article gives clear idea in support of the new users of blogging, that actually how to
do blogging and site-building.

My web site Amir Ali: https://venorex.wordpress.com/
Quote
 
 
0 #143 Amir Ali 2018-04-07 06:16
hello there and thank you for your information – I have certainly picked upp anything new from right here.
I did however expertise some technical points using this web site, as I
experienced to reload the website many times previous
to I could get it to load properly. I had been wondering if your hosting is OK?

Not that I'm complaining, but sluggish loading instances times will very frequently affect your placement
in google and could damage your quality score
if advertising and marketing with Adwords. Well I am adding this RSS to
my e-mail and can look out for a lot more of your respective fascinating content.

Ensure that you update this again very soon.

my blog Amir Ali: https://swrbook.blogspot.com/
Quote
 
 
0 #142 mezo 2018-03-27 05:44
I ѡas suggested tһiѕ bog by way off mʏ cousin. I am now
nnot ѕure whеther tһis submit іѕ writfen by ѡay օf him
as no one else knoѡ suⅽh designated ɑbout my difficulty.
You're amazing! Tһanks!

Feel free to surf to my weblog; mezo: http://mezocoupons.blogspot.com/
Quote
 
 
0 #141 mezo 2018-03-27 02:44
Whаt ɑ stuff of ᥙn-ambiguity аnd preserveness ⲟf valjable experience oon tһe topic of
unexpected feelings.

Aⅼso visit myy web blog: mezo: http://mezocoupons.blogspot.com/
Quote
 
 
0 #140 gratis neuken 2018-02-16 22:26
Bedenk welke indruk je ermee wilt geven.
Quote
 
 
0 #139 Htvirrist 2018-01-23 23:52
ok’ emergency loan payday loans online direct lenders only cash 1 payday loan payday loans online direct lenders only: https://paydayloans-online.us.org/ http://paydayloans-online.us.org/ OK’
Quote
 
 
0 #138 ecu stepper motor 2017-12-13 10:11
I'm curious tto find out what blog platform you have been utilizing?
I'm experiencing some minor security issues wijth my laqtest website and I'd like to find something more safeguarded.
Do you have any solutions?
Quote
 

Add comment

No bad words.


Security code
Refresh


New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1859 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2660 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3314 Hits
gpsim tutorial Machine Learning and Robotics
Read More 8060 Hits

Most read content

New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1859 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2660 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3314 Hits
gpsim tutorial Machine Learning and Robotics
Read More 8060 Hits
Design by i-cons.ch / etosha-namibia.ch