Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
<?php
//say file to download is ../dir/file.doc
// self-managing page might look like this
$timelimit = 5; //in minutes
if (!isset($_GET['DLID']))
{
displaystartpage();
}
else
{
if (validdownload() === true)
{
unset ($_SESSION['time']);
downloadfile($_GET[DLID]);
}
else
{
unset ($_SESSION['time']);
die("This download link has expired");
}
}
function validdownload()
{
session_start();
global ($timelimit);
$mins = $timelimit * 60; //this converts to seconds
$elapsedtime = time() - $_SESSION['time'];
if ($elapsedtime > $mins)
{return false;}
else
{return true;}
}
function downloadfile($id)
{
$dir = "../dir/"; //need to include the trailing slash
switch ($id)
{
case 1:
$file = "file.txt";
if (!file_exists($dir.$file))
{ die ("File does not exist");}
$len = filesize($dir.$file);
header("Content-Type: application/octet-stream");
$header="Content-Disposition: attachment; filename=$file;";
header($header);
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len);
readfile($dir.$file);
break;
default:
die ("No such download exists");
break;
}
}
function displaystartpage()
{
session_start();
global ($timelimit);
$_SESSION['time'] = time();
echo "Click <a href='$_POST[PHP_SELF]?DLID=1'>here</a> to download file. You have $timelimit minutes to do so.";
}
?>