Skip to content

Instantly share code, notes, and snippets.

@inkeliz
Forked from TiuTalk/snippet.php
Last active March 13, 2017 23:06
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 inkeliz/d61ef5d9aba3ba3f0acd4f08bde46a6c to your computer and use it in GitHub Desktop.
Save inkeliz/d61ef5d9aba3ba3f0acd4f08bde46a6c to your computer and use it in GitHub Desktop.
<?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