Tengo preparado algo muy similar para utilizarlo en mi web. A ver si te sirve:
index.html Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="estilo.css" />
</head>
<body>
<ul>
<li><a href="download.php?f=fichero1.pdf">Fichero 1</a></li>
<li><a href="download.php?f=fichero2.pdf">Fichero 2</a></li>
</ul>
</body>
</html>
Como verás, los nombres reales de los ficheros están encriptados. En el ejemplo no uso BD:
La variable TIENE_PERMISO haría referencia a si ha hecho login correctamente.
download.php Código PHP:
<?php
$f = $_GET["f"];
if ($f == "fichero1.pdf") $f = "a977e605f70a68dbc2e47c2e3b2d25b2.pdf";
else if ($f == "fichero2.pdf") $f = "f598551dd30d80e4c215176bb35f47d0.pdf";
$path = "ficheros/" . $f;
$type = "";
$TIENE_PERMISO = true;
if (is_file($path) && $TIENE_PERMISO) {
// Definir headers
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=" . $_GET["f"]);
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($path));
// Descargar archivo
readfile($path);
}
else echo "No se ha encontrado el archivo.";
?>
Los ficheros están en el directorio /ficheros, donde está también el siguiente .htaccess:
.htaccess
Código:
AddType application/octet-stream .pdf
RewriteEngine on
RewriteRule ^(.*)$ ../error-403.php
error-403.php Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="estilo.css" />
</head>
<body>
<h1>Error 403.</h1>
</body>
</html>