antee Posted October 1, 2018 Share Posted October 1, 2018 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted October 1, 2018 Share Posted October 1, 2018 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'); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.