• 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 #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
 
 
0 #168 DustinBold 2018-05-20 17:31
I have checked your site and i have found some
duplicate content, that's why you don't rank high in google's search
results, but there is a tool that can help you to create 100% unique content, search
for; SSundee advices unlimited content for your blog
Quote
 
 
0 #167 Eyelashes growth 2018-05-20 01:02
I was wondering if you ever considered changing the layout of
your site? Its very well written; I love what youve got to say.

But maybe you could a little more in the way of content so people could
connect with it better. Youvee got an awful lot oof text for only
hwving 1 or two images. Maybve you ciuld space it outt better?


Here is mmy weblog: Eyelashes
growth: https://bit.ly/2Is2FRs
Quote
 
 
0 #166 Grow faster 2018-05-19 07:11
Greetings! Very useeful advice witgin this post!

It's the litttle changes that make the biggest changes.
Thankls a lot for sharing!

Review mmy blog post: Grow faster: https://bit.ly/2Is2FRs
Quote
 
 
0 #165 Eye Lashes 2018-05-17 13:16
I believe this is one of the such a lot important information for me.
And i am gllad reading your article. But should remmark on some general issues,
The website style is wonderful,the articles is actually
excellent : D. Excellent process, cheers

my web page Eye Lashes: 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 1904 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2712 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3411 Hits
gpsim tutorial Machine Learning and Robotics
Read More 8391 Hits

Most read content

New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1904 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2712 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3411 Hits
gpsim tutorial Machine Learning and Robotics
Read More 8391 Hits
Design by i-cons.ch / etosha-namibia.ch