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.

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?

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? :slight_smile:

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.


if($id) {



    $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;


The best idea IMO is to store the path, image/file type maybe even the mime-type in separate fields in your DB.


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 :wink:

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 :slight_smile:

Moved to a more appropriate forum. See Where should I post my thread? for details

Sean :slight_smile:

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)

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!



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

that a good idea - can u give me an example