goodQuestion
goodQuestion

Reputation: 11

php pdf download - can't 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?

Upvotes: 0

Views: 2241

Answers (3)

Joseph118
Joseph118

Reputation: 505

did you add the extension with the file name? like this...

$filename = "filename.pdf";
$filepath = "/download/".$filename;

if (file_exists($filePath) {
header('Content-Disposition: attachment; filename='.basename($filepath));
header('Content-type: application/zip' );
header('Content-Length: '. filesize($filePath));
readfile($filepath);
exit;
}

Upvotes: 1

DBC
DBC

Reputation: 72

Sorry, I can't comment because of my low reputation. That is why posting as answer. You could try with

header('Content-type: application/pdf' );
header('Content-disposition: attachment; filename='.$filename.'.pdf');
header('Content-disposition: attachment; filename='.$filename.'.pdf');

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 157967

You are using the wrong Content-Type header. Use

header('Content-type: application/pdf' );

It is defined in RFC 3778

Upvotes: 1

Related Questions