-1
I have some pdf files that are generated by my system that I know work, but when I do a script to download this file, this file gives me opening error.
The script below downloads the file my system generates
// Definimos o novo nome do arquivo
$novoNome = 'imagem_nova.pdf';
// Configuramos os headers que serão enviados para o browser
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$novoNome.'"');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($aquivoNome));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Expires: 0');
// Envia o arquivo para o cliente
readfile($aquivoNome);
Second attempt
$arquivo = $_GET["arquivo"];
if(isset($arquivo) && file_exists($arquivo)){
// faz o teste se a variavel não esta vazia e se o arquivo realmente existe
switch(strtolower(substr(strrchr(basename($arquivo),"."),1))){
// verifica a extensão do arquivo para pegar o tipo
case "pdf": $tipo="application/pdf"; break;
case "exe": $tipo="application/octet-stream"; break;
case "zip": $tipo="application/zip"; break;
case "doc": $tipo="application/msword"; break;
case "xls": $tipo="application/vnd.ms-excel"; break;
case "ppt": $tipo="application/vnd.ms-powerpoint"; break;
case "gif": $tipo="image/gif"; break;
case "png": $tipo="image/png"; break;
case "jpg": $tipo="image/jpg"; break;
case "mp3": $tipo="audio/mpeg"; break;
case "php": // deixar vazio por seurança
case "htm": // deixar vazio por seurança
case "html": // deixar vazio por seurança
}
header("Content-Type: ".$tipo);
// informa o tipo do arquivo ao navegador
header("Content-Length: ".filesize($arquivo));
// informa o tamanho do arquivo ao navegador
header("Content-Disposition: attachment; filename=".basename($arquivo));
// informa ao navegador que é tipo anexo e faz abrir a janela de download,
//tambem informa o nome do arquivo
readfile($arquivo); // lê o arquivo
exit; // aborta pós-ações
}
?>
The way it is above you can even get the content... but pull a strange content to the file that downloads
Both with your comment code, with the above code it results in my page html plus content pdf
Friend, this way he makes a download of file of 0 bytes, IE, does not transfer the contents of the original file to the one that will be made the download.
– gabrielfalieri
So, you need to evaluate the path/folder of your pdf. In your code above there were also 2 variables with names changed. If you are downloading empty file, it is pq is not getting the file properly. Remove the basename and run the code again.
– Gnomo Escalate
The way I know is right
– gabrielfalieri
I use in my projects only the
Content-Type
,Content-Length
andContent-Disposition
asheaders
andreadfile($arquivo);
exit();
Exit avoids downloading anything there is after it.– Gnomo Escalate
I’ll see if this Xit solves
– gabrielfalieri
He keeps picking up some html, I’ll put it in the code
– gabrielfalieri
I added the code I use in my projects. Use a
if(isset($novoNome) && file_exists($novoNome)){...}
to make sure the file is being sent.– Gnomo Escalate
Let’s go continue this discussion in chat.
– gabrielfalieri