Something very simple, to have a minimum sense of what you really need to do, is this:
<?php
// index.php
// apresentar os respectivos links
print "<a href=\"download/{$item['chave']}\" target=\"_blank\">{$item['nome']}</a>";
print "<a href=\"download/{$item['chave']}\" target=\"_blank\">{$item['nome']}</a>";
// verificar se o link foi clicado
$a = isset($_GET['a']) ? $_GET['a'] : null; // a=download
$f = isset($_GET['f']) ? $_GET['f'] : null; // f=chave_no_banco
if(isset($a) && isset($f)){
/* algo melhor elaborado aqui,
para verificar os detalhes da chave e retornar
dados correspondentes do banco antes de iniciar a transferencia,
ou algo semelhante
*/
print "A transferir: <br/>";
print "<strong>ficheiro: </strong>{$f}<br>";
sleep(1);
include_once 'download.php';
}
To the htaccess you could add this:
RewriteRule ^download/([A-Za-z0-9]+)$ index.php?a=download&f=$1
To download the file in question:
<?php
$name= $_GET['f'];
$randNumber = date('d-m-Y');
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=\"" . basename($randNumber.$name) . "\";");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($name));
ob_clean();
flush();
readfile("_directorio_do_arquivo_no_servidor/".$name);
exit;
?>
NOTE: Although without something functional, I do not say for now that it is recommendable, there are procedures that you must still implement and several modifications to be made, for it to be implemented successfully. It shall also consult the documentation of the apache.
References