0

I want to open a pdf file that is uploaded by the user in the browser itself. I have already tried all the solutions from the similar thread.. but it keeps showing download popup.

<?php
        $filename = $_REQUEST['filename'];

            /*echo $filePath = 'upload/register/'.$filename;
            if (!file_exists($filePath)) {
                echo "The file $filePath does not exist";
                die();
            }


            header('Content-type:application/pdf');
            header('Content-disposition: inline; filename="'.$filename.'"');
            header('content-Transfer-Encoding:binary');
            header('Accept-Ranges:bytes');
            readfile($filePath);
            */

        /*header("Content-type: application/pdf");
        header("Content-Disposition: inline; filename=$filename");
        @readfile('upload\register\$filename');*/

        /*echo "<iframe src=upload\"register\"$filename\" width=\"100%\" style=\"height:100%\"></iframe>";*/
            ?>
            <!DOCTYPE html>
            <html>
            <head>
                <title>PDF</title>
            </head>
            <body>
<embed src="upload/register/<?=$filename?>" type="application/pdf" width="100%" height="600px" />
<iframe src="upload/register/<?php echo $filename; ?>" width="90%" height="500px">
            </iframe>
            </body>
            </html>

i have also tried embed tag.. but nothing seems to work and it doesnt open and downloads instead

  • Not all browsers are able to open PDF files. It depends if suitable support for the PDF format is available in the browser and is switched on. The user can always disable the feature even if it is installed. Which browser(s) have you tested this code with? Have you checked the settings to ensure that the browser has a PDF plugin and that opening of PDFs inline is enabled? – ADyson Jun 03 '20 at 16:38
  • I'm curious here. You say it "downloads". How are you running this as, using an HTTP protocol or directly into your browser as `file:///`? Your question is unclear. – Funk Forty Niner Jun 03 '20 at 16:45
  • @FunkFortyNiner on button click m opening this page , the code that is mentioned above. And file stored in xampp server and i want to open it in webpage instead of downloading – Monika Nishad Jun 03 '20 at 16:55
  • 1
    @MonikaNishad That doesn't answer Funk's question. Please provide a direct answer to the very specific question which was asked. – ADyson Jun 03 '20 at 16:58
  • @ADyson yes i have ensured that already.. and i have tested in some other website where pdf is opening in the browser – Monika Nishad Jun 03 '20 at 16:58
  • @ADyson i have showed all the codes that i have already applied from the previous solution as a comment – Monika Nishad Jun 03 '20 at 16:59
  • @ADyson i have mentioned that files is in xampp server so m opening in localhost not file:/// – Monika Nishad Jun 03 '20 at 17:01
  • ok thanks. To be honest it looks like in theory you have done everything correctly. If you're serving over HTTP, you've set the path correctly of the PDF, then it should work. Even the version you gave with the headers should also be ok. But if it's still downloading the PDF file instead of showing it inline, I would strongly suspect the browser's settings. I know you said you've checked them, but check again. Also please try your code in other browsers. You didn't say which browser you tested with, but Chrome and Firefox usually come with PDF display enabled. – ADyson Jun 03 '20 at 17:16
  • Unless you mean it's the **PHP code** which gets downloaded, rather than the PDF? It wasn't 100% clear actually. But if you're running XAMPP then really that shouldn't be a problem, if your code file is named with a .php extension and the PHP setup in XAMPP was successful. – ADyson Jun 03 '20 at 17:36
  • @ADyson i m doing the testing both in firefox developer edition and also latest chrome. and yes that particular pdf is getting downloaded – Monika Nishad Jun 03 '20 at 17:43
  • @FunkFortyNiner I think this is the wrong dupe, from what is being said – ADyson Jun 03 '20 at 18:10
  • @ADyson Ok, the OP didn't respond to me earlier as to *how* they used their files, only to you. I wonder why that was. I reopened. – Funk Forty Niner Jun 03 '20 at 18:11
  • 1
    @FunkFortyNiner thanks. Unfortunately I still can't see anything _else_ wrong with the situation from what's been provided but maybe someone else will come along and spot it. – ADyson Jun 03 '20 at 18:14

0 Answers0