Skip to content

Instantly share code, notes, and snippets.

@juananruiz
Created November 3, 2011 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juananruiz/1335934 to your computer and use it in GitHub Desktop.
Save juananruiz/1335934 to your computer and use it in GitHub Desktop.
Protegiendo la descarga de un fichero
<a href="descarga.php?archivo=imagen5.jpeg" >Descargar imagen</a>
<?php
//Si la variable archivo que pasamos por URL no esta
//establecida acabamos la ejecucion del script.
if (!isset($_GET['archivo']) || empty($_GET['archvo'])) {
exit();
}
//Utilizamos basename por seguridad, devuelve el
//nombre del archivo eliminando cualquier ruta.
$archivo = basename($_GET['archivo']);
$ruta = 'imagenes/'.$archivo;
if (is_file($ruta))
{
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.$archivo);
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($ruta));
readfile($ruta);
}
else
exit();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment