Display pdf on website

2019-07-22 20:43发布

问题:

I've a little problem. On my server i've several pdf files saved outside the public folder. I want to display those files on my website.

The code to display the pdf I use now:

$pdf = '/var/www/vhosts/domain.com/users/'.$_GET['file']; 
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'.$pdf.'"'); 
readfile($pdf);

What I do now is display them via an iFrame but on IE8 i'll get the message to save the file to my computer. On chrome and IE9 there are no problems.

Is there a better way to display the pdfs?

回答1:

please look up this question, the user had the same question as you: Show a PDF files in users browser via PHP/Perl

I also have a security notice for your:

THIS: $pdf = '/var/www/vhosts/domain.com/users/'.$_GET['file']; Can be extremly dangerous, imagine if some one modifies your url parameter like this: yoursite.php?file=../../index.php

If you are not very careful here, people can download other files from your server... so please be careful and validate your $_GET['file'] before using it.

Regards



回答2:

I think if you leave you: content-disposition: inline and change it to content-disposition: filename="'.$pdf.'" the browser will determine what to do with the PDF. If there is a plugin for pdf installed it will use that one.



回答3:

$pdf = '/var/www/vhosts/domain.com/users/'.$_GET['file']; 
header('Content-Disposition: inline; filename="'.$pdf.'"');// download header 
readfile($pdf)  //read our file and shows on website


标签: php pdf iframe