-
-
Save inkeliz/d61ef5d9aba3ba3f0acd4f08bde46a6c to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
const LOCAL_ARQUIVO = 'pasta/do/arquivo/'; | |
$nomeArquivo = 'imagem.jpg'; | |
if(!preg_match('/^(?:[a-z0-9_-]|\.(?!\.))+$/iD', $nomeArquivo)){ | |
echo 'Arquivo é inválido'; | |
exit; | |
// Isto é feito para evitar ataques para ler aquivos internos (ex. "../etc/"...), na pior das hipóteses. | |
} | |
if(!file_exists(LOCAL_ARQUIVO.$nomeArquivo)){ | |
echo 'Arquivo não existe'; | |
exit; | |
} | |
// Configuramos os headers que serão enviados para o browser | |
header('Content-Description: File Transfer'); | |
header('Content-Disposition: attachment; filename="'.$nomeArquivo.'"'); | |
header('Content-Type: application/octet-stream'); | |
header('Content-Transfer-Encoding: binary'); | |
header('Content-Length: ' . filesize(LOCAL_ARQUIVO.$nomeArquivo)); | |
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); | |
header('Pragma: public'); | |
header('Expires: 0'); | |
readfile(LOCAL_ARQUIVO.$nomeArquivo); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment