0

What i have done is i have put direct access restriction on one of the directories that contain pdf. Now i have to open the files in iframe. when i create a copy of file and pass that files address to iframe src it downloads instead of opening the file.

below is my code.

<?php
    session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <title><?php echo $_GET['filename'] ?></title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
    <?php
        $mime = mime_content_type($_GET['filename']);
        if(strstr($mime, "image/")){
    ?>
            <img src="fileViewer.php?filename=<?php echo $_GET['filename'] ?>" style="width: 70%;margin-left: 15%;">
    <?php
        }
        else if(strstr($mime, "application/pdf")){
            $myfile = fopen("Temps/".$_SESSION['userName']."file.pdf", "w");
            fwrite($myfile, file_get_contents($_GET['filename']));
            $fileName= "Temps/".$_SESSION['userName']."file.pdf";
    ?>
    <iframe src="<?php echo $fileName ?>" style="width: 100%;height: 100vh;"></iframe>

    <?php
        }
        else{
    ?>
    <iframe src="fileViewer.php?filename=<?php echo $_GET['filename'] ?>" style="width: 100%;height: 100vh;"></iframe>
    <?php
        }
    ?>
</body>
</html>
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Does this answer your question? [Show a PDF files in users browser via PHP/Perl](https://stackoverflow.com/questions/4679756/show-a-pdf-files-in-users-browser-via-php-perl#answer-41206252) – Definitely not Rafal Jun 17 '21 at 10:30
  • Does this answer your question? [Show a PDF files in users browser via PHP/Perl](https://stackoverflow.com/questions/4679756/show-a-pdf-files-in-users-browser-via-php-perl) – Joe Jun 17 '21 at 11:55
  • no that my code is already doing it downloads the pdf but i want to show the pdf in browser like embed. – Kamran Ali Raza Sandrana Jun 17 '21 at 12:00
  • Please do not edit solution announcements into the question. Accept (i.e. click the "tick" next to it) one of the existing answer, if there are any. You can also create your own answer, and even accept it, if your solution is not yet covered by an existing answer. – Yunnosch Jun 17 '21 at 12:34

2 Answers2

0

I have Updated my code to the below code and all got resolved..

    session_start();
?>
    <?php
        $mime = mime_content_type($_GET['filename']);
        if(strstr($mime, "image/")){
    ?>
            <img src="fileViewer.php?filename=<?php echo $_GET['filename'] ?>" style="width: 70%;margin-left: 15%;">
    <?php
        }
        else if(strstr($mime, "application/pdf")){
        $file = $_GET['filename'];
        $filename = $_GET['filename']; /* Note: Always use .pdf at the end. */
        header('Content-type: application/pdf');
        header('Content-Disposition: inline; filename="' . $filename . '"');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . filesize($file));
        header('Accept-Ranges: bytes');
        @readfile($file);

    ?>
    <?php
        }
        else{
    ?>
    <iframe src="fileViewer.php?filename=<?php echo $_GET['filename'] ?>" style="width: 100%;height: 100vh;"></iframe>
    <?php
        }
    ?>

Thank you all

-1

If your file is not on the same host as your website then the expected behaviour of the browser is to download the file. To allow opening that file, make sure your website and file are hosted on the same server.

NavalRishi
  • 124
  • 9