Jump to content

Display PDF - Headers already sent


antee

Recommended Posts

I’m a newbie…So thanks for any suggestions…  I’m trying to display a PDF in the browser when a link is clicked using PHP.

I’ve used a simple href link and it works fine – except that the permissions on the file have to be wide open for the href link to work (and the information in the PDF is private). So I’ve tried the following PHP code that I saw on-line…

<?php

$file = "test.pdf";

$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension

$mime = finfo_file($finfo, $file);

header('Pragma: public');

header('Expires: 0');

header('Content-Type: $mime');

header('Content-Description: File Transfer');

header('Content-Disposition: attachment; filename="'.basename($file).'"'));

header('Content-Transfer-Encoding: binary');

header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

header('Content-Length' . filesize($file));

ob_clean();

flush();

readfile($file);

?>

 

Although this page does not present any output before this script runs – the Wordpress theme displays the page name. So I’m getting “Headers already sent..” error.

Is there any other way to display a PDF file in a secure way without using header or href link?

Link to comment
Share on other sites

Don't just ignore the "headers already sent" error. What is it talking about? Where did the output start and where does this code you've shown fit into it?

Also, don't bother with finfo. You already know the content type.

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

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.