0

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.

NajLinus
  • 147
  • 2
  • 3
  • 10

1 Answers1

2

Instead of

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

Try this

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

Also review this:

Community
  • 1
  • 1
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • Hello Michael_B, I am having problem while using japanese character on file name. My file name is mix of english and japanese character. For example $file="form申し込み用(data-form)_DOP.pdf". When I change my file name to only english then everything works well. But when I mix japanese and english character same error occurs which I have mentioned above. I thought that the problem occurred because my PDF file was corrupted. But that doesn't seems to be the real problem. I have recreated the PDF file. Can you give me any suggestion please? – NajLinus Jul 25 '16 at 06:32