Open PDF in a browser using PHP

I do:

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
header('Content-Length: ' . filesize($file));
@readfile($file);

and it works great. PDF opens in a browser.

Is it possible to do if-then-else earlier? I tried and I get raw PDF

%PDF-1.2 9 0 obj << /Type/Font /Subtype/Type1 /Name/F1 /FontDescriptor 8 0

Why does if-then-else change headers?

thank.

+3
source share
2 answers

It would be helpful if you would place your if-then-else code exactly. I assume you have some white space output before the output of the headers.

0
source

Make sure that the code in the if-then-else block does not display anything on the page.

As soon as something comes out, the calls header()will no longer work.

0
source

Source: https://habr.com/ru/post/1786730/


All Articles