Enviando headers de download

Categoria: PHP
Publicado em 28 de Novembro de 2010

Exemplo:

1
2
3
4
5
6
7
8
9
$filesize = filesize( $filename );
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: '. $filesize);

Para enviar o conteúdo do arquivo:

1
2
echo file_get_contents( $filename );
die();

 

Opcionalmente, a função readfile pode ser usada para ler um arquivo e fazer output dos dados.

 

 

Copyright © Fernando Hidemi Uchiyama 2010 - Todos os direitos reservados