简体   繁体   中英

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. 在此处输入图片说明

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.

Instead of

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

Try this

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

Also review this:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM