NajLinus NajLinus - 4 months ago 34
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?


download.php

<?php
$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));
ob_clean();
flush();
readfile($filepath);
exit;
?>


Updated:

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

Update2:

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

Instead of

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

Try this

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

Also review this: