View PDF & Doc Files Without Downloading them

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abramfas
    New Member
    • May 2013
    • 9

    View PDF & Doc Files Without Downloading them

    Hello guys,

    I have been able to work on this code in this article uploading files into MySQL database using php and it is working well.

    But now I want to be able to allow users just view the PDF or doc files without having to download them.

    Please I need help on this.

    Thanks
    Last edited by Frinavale; May 30 '13, 01:34 PM. Reason: Moved question off of the article thread into a thread of its own. Put a reference to the article that it was moved off of and fixed grammar.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Hi Abramfas,

    This is going to depend on the browser.

    If the browser knows how to open the file, then it will do so. If the browser does not know what to do it will give the user the option to download the file so that they can open the file in an application that does know how to open it.

    For example, if the browser has a plug-in installed that allows users to view PDF files, it will use the plugin to display the file. If the browser does not have a plugin installed, it will popup a dialog that lets the user save the file to their computer so they can open it with an application that knows how to view PDF files.

    If you want to display the file on the same page as your content, consider using an <iframe> who points to the PDF file that you wish to let the user view. This way the browser will display the file using the appropriate plugin if it can, or else the user will be prompted to download the file.

    You could also open a new window if you are not comfortable using <iframe>s.


    -Frinny

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      Abramfas,

      Are you trying to emulate what google mail does with attached documents, rendering them as HTML? Or, are you simply trying to display the full-document using a plug-in, as Frinny described?

      --Oralloy

      Comment

      • abramfas
        New Member
        • May 2013
        • 9

        #4
        Orally,

        I am trying to display the full document with a plugin.

        Frinavale,

        How do I insert an iframe am a bit new in php programing

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          Abramfs,

          Sounds like you are on the right path, then. I'll step out.

          From reading your request, I didn't know what your goal was. I've had folks want to do limited display of documents, as pre-sales teasers, so I thought I'd ask.

          As for iframes, they are a type of HTML tag, this page at W3Schools can give you a bit of information.

          Luck!
          Oralloy

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            The link that @Oralloy posted is a good starting point for learning about iframes.

            You would insert an iframe the same way as you would insert any HTML using php.

            -Frinny
            Last edited by Frinavale; May 31 '13, 06:32 PM.

            Comment

            • Luuk
              Recognized Expert Top Contributor
              • Mar 2012
              • 1043

              #7
              But "View PDF & Doc Files Without Downloading them"
              will never be possible,

              Because you need to download something to be able to view anything.......

              ;)

              Comment

              • abramfas
                New Member
                • May 2013
                • 9

                #8
                Thannks guys,

                But am still not able to resolve the issues. I am trying to view the PDF inline let me show you the edited vasion am working with.

                Code:
                <?php
                 $server = 'localhost';
                 $user = 'root';
                 $pass = '';
                 $db = 'database';
                 
                 // Connect to Database
                 $connection = mysql_connect($server, $user, $pass) or die ("Could not connect to server ... \n" . mysql_error ());
                 mysql_select_db($db) or die ("Could not connect to database ... \n" . mysql_error ());
                        $id = intval($_GET['id']);
                        $file=  'SELECT `name`, `mime`,`size`, `created` FROM `tablename`';
                        $result = mysql_query($file);
                
                if($d = mysql_fetch_array($result))
                {
                	$file = $d['name'];
                	header('Content-type: application/pdf');
                	header("Content-Disposition: inline; name=".$row['name']);
                	header('Content-Transfer-Encoding: binary');
                	header('Content-Length: ' . size($file));
                	header('Accept-Ranges: bytes');
                	header("Location: $file");
                	@readfile($file);
                }
                else{
                	echo'No file with the specified ID exists';
                }
                ?>

                Comment

                • Luuk
                  Recognized Expert Top Contributor
                  • Mar 2012
                  • 1043

                  #9
                  Line 10: What is the use of $id ?
                  Line 18: Where did $id['name'] get a value ?
                  Line 20: 'size()' is an unknown function

                  Comment

                  • shruthi gs
                    New Member
                    • Aug 2016
                    • 2

                    #10
                    is there any optionto preview and print files instead of downloading

                    Comment

                    • Luuk
                      Recognized Expert Top Contributor
                      • Mar 2012
                      • 1043

                      #11
                      @shruthi gs: if you have any question, please create a NEW question.
                      Do NOT pollute an old thread with a new question...

                      Comment

                      Working...