Download pdf file from php error

Asked

Viewed 336 times

-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);

inserir a descrição da imagem aqui

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

1 answer

-1


The problem above was in the location of the code responsible for the download, as there was html below the code the same would download all the content of the page, it was indicated in the comments move code to a unique php page downloads.php, for example.


PREVIOUS ATTEMPTS

This can be a problem in encoding your pdf when it is generated or when it is uploaded to your folder on the server, the way your file is processed may be interfering with it and conflicting with your download script. If your download is always a file pdf I would trade.

//De
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$novoNome.'"');
header('Content-Type: application/octet-stream');
//Para
header("Content-Disposition: attachment; filename=".$novoNome);
header('Content-Type: application/pdf');

Another issue is that these lines are with the wrong variable $aquivoNome change to $novoNome.

header('Content-Length: ' . filesize($novoNome));
readfile($novoNome);

The application/octet-stream is a standard type for all other cases. An unknown file type should use this type. Browsers will take more care when manipulating these files, trying to protect the user and prevent dangerous behaviors. SOURCE: Mozilla.org

EDIT

This is the code I use to download my pdfs, they are generated dynamically using FPDF and FPDI.

if(isset($novoNome) && file_exists($novoNome)){
   header("Content-Type: application/pdf");
   header("Content-Length: ".filesize($novoNome));
   header("Content-Disposition: attachment; filename=".basename($novoNome));
   readfile($arquivo);
   exit(); 
}
  • 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.

  • 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.

  • The way I know is right

  • I use in my projects only the Content-Type, Content-Length and Content-Disposition as headers and readfile($arquivo); exit(); Exit avoids downloading anything there is after it.

  • I’ll see if this Xit solves

  • He keeps picking up some html, I’ll put it in the code

  • 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.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.