Created
November 3, 2011 06:46
-
-
Save juananruiz/1335934 to your computer and use it in GitHub Desktop.
Protegiendo la descarga de un fichero
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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