Thursday, 30 August 2018

php pdf download - can not open the downloaded file correctly

On my webpage is the possibilty to download a pdf-file (285kB). The problem is that you download the website as a pdf and not the pdf-file I want to be downloaded.

So I'cant open the downloaded pdf with Adobe Reader but have to open the file with notepad and the content is
<!DOCTYPE html><html lang="en"><he...

Here is the Code for downloading the file:
 $filepath = "/download/";
    $filename = "filename";
    header('Content-disposition: attachment; filename='.$filename);
    header('Content-type: application/octet-stream' );
    header('Content-Length: '. filesize($filepath.$filename));
    readfile($filepath.$filename);
    exit;

Can you help me?

You are using the wrong Content-Type header. Use
header('Content-type: application/pdf' );

It is defined in RFC 3778

0 comments:

Post a Comment