SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Hiding from the world
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    displaying PDFs from mysql database with php

    Hi, not sure whether this is a php or mysql question. iam using mysql to store some report files that people may want to look at on my website. They are mainly word.docs but i have a few pdfs that i want to display. The word files work fine when i call them but the pdfs just open dreamweaver on my machine and display as code, or if i save as then it saves it as the php display page i have, which is no use. Why don't the pdfs open properly and how do i cure it. Your help will be much appreciated.

  2. #2
    SitePoint Columnist
    Join Date
    Nov 2003
    Location
    Ohio
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I may be reading this wrong - but are you storing the path to the pdf file in the database table field? I.e. in a table field called filepath that stores the path tot the pdf - something relative like "media/nameoffile.pdf" so when you call it you can do so within an a href using php?
    Freelance System Administrator, Researcher, Writer
    Practical Applications
    Open Sourcery "SitePoint's Open Source Blog"

  3. #3
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Exactly how are you calling them? Do the PDF files have a .pdf file suffix on them when you select the link or howsoever you access/retrieve them?

    Without wanting to sound condesending - but do you have a PDF reader on the machine you wish to open them on like Adobe Acrobat Reader?

  4. #4
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Hiding from the world
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for the replies, Iam very new to php and mysql so i may have misunderstood what i can actually do with mysql as i was going to store the pdf as binary data within the mysql table. Ive tried it with some word files and that works fine. Is this something that shouldn't be done?. It would make it easier for people in my office to add documents they have done to the website. I do have acrobat reader working on my machine (i don't blame you for asking its always worth checking the simple things first). The display page i am using is below. Thanks for you help.

    Code:
    <?php
    
    if($id) {
    
        @MYSQL_CONNECT("abc","def","ghi");
    
        @mysql_select_db("DB1");
    
        $query = "select bin_data,filetype from binary_data where id=$id";
        $result = @MYSQL_QUERY($query);
    
        $data = @MYSQL_RESULT($result,0,"bin_data");
        $type = @MYSQL_RESULT($result,0,"filetype");
    
        Header( "Content-type: $type");
        echo $data;
    
    };
    ?>

  5. #5
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The best idea IMO is to store the path, image/file type maybe even the mime-type in separate fields in your DB.

    Why?

    1: Easier to manage. You can see at a glance in your DB what's "in there" - something that's kinda hard to do with a load of binary data in a BLOB field

    2: Less memory intensive - loading a load of binary data into a large field like a BLOB type takes up a load of memory. Compare that with a few strings of text in a VARCHAR field...

    Regardless - storing it either way - you should be able to download the document in the appropriate format. Make sure the right mime type is beng sent with the link-click.

    PDFs are: application/pdf - so in PHP you would use header("Content-Type: application/pdf"); You may have to futz around with it to get it to work though... you always do...

    See this page - I've found it very useful in the past.
    Good luck

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Moved to a more appropriate forum. See Where should I post my thread? for details

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  7. #7
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Hiding from the world
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help. I ve had a look and the filetype is application/pdf but it still doesn't lauch acrobat when i try and open it. I'll have a play around and see what happens. Cheers

    (Sorry i got the wrong forum section)

  8. #8
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    epsom
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am really getting to grips with php now.

    The only problem that I am having which I cannot find the answer anywhere is with the use of pdf.

    I have a form populate a pdf and have created all the lines with x and y coordinates but what do I do when a field goes over a certain number of characters and the line does not go onto the next one and goes too far across the screen.

    How do I tell it to go onto the next line if there is not enough space?

    Please help!

    Thanks

    Nicholas

  9. #9
    SitePoint Zealot
    Join Date
    Feb 2001
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use strlen() to get the number of characters in the string to see if it's too many for a single line. Then you can use substr() to put sections of the strings on each line
    Guava

  10. #10
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    epsom
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that a good idea - can u give me an example


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •