The idea is to create a table with a unicoid field.
A page that will insert $ key = uniqid (md5 (rand ())); in the unicoid field and generate a link download.php? id=". $ key
On the download page, obtain the database key equal to the value of the id parameter. If it exists, force the browser to download and immediately give a delete in the unicoid field.
downloadkey.php
$link = new mysqli ("localhost", "USUARIO", "SENHA", "DB");
if(empty($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
}
$url = preg_replace('/\?.*$/', '', $_SERVER['REQUEST_URI']);
$folderpath = 'http://'.$_SERVER['HTTP_HOST'].'/'.ltrim(dirname($url), '/').'/';
$key = uniqid(md5(rand()));
$time = date('U');
echo "<p>Download link: <a href=\"" . $folderpath . "download.php?id=" . $key . "\">Baixar</a></p>";
echo "<p><span class=\"box\">" . $folderpath . "download.php?id=" . $key . "</span></p>";
$sqli = ("INSERT INTO downloadkey (uniqueid,timestamp) VALUES(\"$key\",\"$time\")");
$result= mysqli_query($link,$sqli);
mysqli_close($link);
Download.php
$link = new mysqli ("localhost", "USUARIO", "SENHA", "DB");
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
$query = ("SELECT * FROM downloadkey Where uniqueid='".$_GET['id']."'");
$result = mysqli_query($link,$query);
if (mysqli_num_rows($result) > 0) {
$arquivo = 'arquivo.zip';
header('Content-type: octet/stream');
header('Content-disposition: attachment; filename="'.$arquivo.'";');
readfile($arquivo);
$sql = ("DELETE FROM downloadkey Where uniqueid='".$_GET['id']."'");
$result = mysqli_query($link,$sql);
exit;
}else{
echo "<p>Link de download que você está usando é inválido.";
echo "<br><a href=\"downloadkey.php\">Clique aqui para obter novo link de download</a></p>";
}
mysqli_close($link);