NajLinus NajLinus - 1 year ago 104
PHP Question

Cannot open PDF file downloaded using php

I am using the following php code to make pdf file download. I am able to download the pdf file but cannot open it. What I am doing wrong? Can anyone help me?


$file = 'Rev.pdf';
$filepath = "doc/" . $file;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream; charset=utf-8');
header('Content-Disposition: attachment; filename='.basename($filepath));
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($filepath));


When I try to open it after download on browser, I get following error.
enter image description here


Following is my HTML

<a href="download.php">download</a>

Update3: SOLVED

First of all I want to say sorry to make you all trouble. I have solved the issue. Actually my PDF file itself was corrupted. So I created new pdf file and it's working well. Once again thank you all for hearing my problem and giving suggestions.

Answer Source

Instead of

header('Content-Type: application/octet-stream; charset=utf-8');

Try this

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

Also review this:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download