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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.