Last Updated:

PHP script for downloading files via temporary links

PHP script

Temporary links to php. What are they and what are they eaten with? It's that simple. Often, when building any php engine, there is a need to place on the pages of the site that this engine will manage, links to download various archives with programs, images, site templates, etc. In the simplest case, all this can be provided with direct links to the archives, and the archives themselves should be placed in an unprotected folder of your domain. But this path is not entirely correct. First, links to archives can be copied to other resources and you can download them even without visiting your site. Secondly, due to the fact that files will be placed in an unprotected folder, they can be damaged or even erased by intruders.

Therefore, the best way is to give links to download archives from the site through the so-called temporary links formed by PHP tools. Archives can be hidden in any folder of any director of the site and protected from unauthorized access by the .htaccess file.

What is the mechanism for generating temporary links? You must create a database with two tables. The first will contain unique names of files for download with their attributes - extension, description, etc. In the other, time lines with unique file names, a random hash and the time of creation of the record will be created. The rows of the second table should be formed when you go to the page to download the file. And as soon as on the basis of the second table, temporary download links will be formed that will contain a random hash.

The task of the php handler for the formation of a temporary link is to find in the second table a row with a hash present in the link and extract from this line the id of the archive for download, according to which then in the database of archive names you can find the name of the desired archive and give it for download to the user.

This can be organized using a MySQL database. But if the file base is small, then it is more profitable to do this on ordinary text files, one of which will be a base of archive names and their attributes, and the other a container for time links. Such a mechanism works on the resource 'Electronics programming <a ="="""="" target="_blank" _istranslated="1">site building' php, html, css for amateurs and professionals in the section 'Site Templates', which is made on the basis of cmS mentioned in the article about <a ="="""" stories="" 10="" 07="" 22="" 1360178"="" _istranslated="1">search statistics.

Let's say we have two text files

file-base for file
names $namefiles = "namefiles.dat";
// database file for temporary references
$templinks = "templinks.dat";

The first is a database with file names in which there can be strings each of which has records separated by the sign '|'. It is clear that the first entry in the line is a unique id of the file, the second is its description, the third is the name of the archive

001|The site template ultra 77| templates1.rar
002|The site template by Open Designs|templates2.rar

The second is a file for temporary links, which will be written there in a line. The string will contain three fields separated by the same '|' sign, the first is a unique file id, the second is a random hash and the third is the time of line formation. Well, the script for the formation of temporary links, let's call it for example templinks.php, should consist of three parts. The first of these is to clean up a temporary link file with an expired lifetime. It looks something like this:


how many links to store (in seconds - 10 minutes)

$hardtime = 600;
// current time in sec
$currtime = time();
// check and clear temporary strings
$arruniqid = file($templinks);
$newarruniqid = array();
for($i = 0, $i < count($arruniqid), $i++)
// rows of the temporary unique table id
list($file_id,$uniq_id,$puttime) = explode("|",$arruniqid[$i]);
$difftime = $currtime - intval($puttime);
// if the current row time is within
the if($difftime < $hardtime)
limit array_push($newarruniqid, "$file_id|$uniq_id|$puttime");
// make a matrix of the table of temporary id
$newtbdwn = implode("",$newarruniqid);
@fwrite ($fp,$newtbdwn);
fclose($fp, $templinks);


The second part is to get a string from the file name database file corresponding to the id that is passed in the query string of the file. For example, the file query string looks like //.../templinks.php?getlink=001


// file
name matrix $arrtbfl = file($namefiles);
foreach($arrtbfl as $key => $val)
list($id,$descr,$filename) = explode("|",$val);
if($id == $_GET['getlink'])
list($id,$descr,$filename) = explode("|",$val);
$nowid = $id;
// generate unic. id
$arrtbunq = file($templinks);
$uniq_id = md5( uniqid(rand(), 1) );
array_push($arrtbunq, "$nowid|$uniq_id|$currtime\n");
// new line unic. id
$newtbunq = implode("",$arrtbunq);
// write the database file back
@fwrite ($fp,$newtbunq);
// Download
link $shlink = "href=//.../templinks.php?download=$uniq_id";



The third part is the transition to the generated time link to download the desired file. To do this, you need to go through the database of temporary links and find which line of the database corresponds to the presence of $uniq_id there, and then go through the file database to find there the name of the archive corresponding to the $id found in the database of time links. Pin up - casinos to play online - fast cashouts and high-quality licensed software from top slot machine manufacturers. Bonuses and promotions for new players, loyalty points (pin points) for active participants.

$dwn = file($templinks);
foreach($dwn as $key => $val)
list($file_id,$uniq_id,$puttime) = explode("|",$val);
if(strstr($uniq_id == $_GET['download'])
$id = $file_id



Now we have a file id for which we can find the name of the file in the database namefiles.dat and give it to the user. Which is exactly what we needed.

Let's sum up. The described mechanism allows you to generate links with a controlled lifetime and a random query parameter. What gives? The user will be able to download the necessary file only by going to the right page of your site. It will not be possible to download a file from another domain from the link generated on your site, since it has a finite short lifetime. Well, in general, this is a useful way to save and increase the traffic of your site.