Read PDF from mysql db

G’Day All,

So I’ve started playing around with php and mysql and have managed to store pdf files in mysql db. Now I want to allow users to view the pdf’s and this is where I’m stuck. See code below: (Code from book by Kevin Yank)

----Code Start----
if (isset($_GET[‘action’]) and
($_GET[‘action’]==‘view’) and isset($_GET[‘id’])){

$id = $_GET[‘id’];
$result = mysql_query(“SELECT DocName,DocType,DocSize,DocData
FROM docs
WHERE ID=‘$id’”);
$filedata=MYSQL_RESULT($result,0,“DocData”);
$mimetype=MYSQL_RESULT($result,0,“DocType”);
$filesize=MYSQL_RESULT($result,0,“DocSize”);
$filename=MYSQL_RESULT($result,0,“DocName”);
$disposition = ‘inline’;
header(“Content-length: $filesize”);
header(“Content-type: $mimetype”);
header(“Content-disposition: $disposition; filename=$filename”);

echo $filedata;

exit();

}
------Code End------

When I click the link, Acrobat reader opens and displays the following error:
“There was an error opening this document. This file cannot be found.”

Any help in solving this problem will be appreciated, thanks.

Hi biermal, welcome to the forums.

What is the datatype of the DocData field?

Thank you,
The DocData is a MediumBloB type.

The mediumblob can hold over 16MB so unless your files are larger than that I’m guessing you either stored bad data, i.e. didn’t use readfile, or you need another header or more.

You may need cache header(s) (the file not found error?)

header('Cache-Control:');
header('Pragma:');

but I expect that a more important header is

header("Content-Transfer-Encoding:* binary");

I think it won’t solve your problem, but you didn’t quote the filename, like so


header("Content-disposition: $disposition; filename=\\"$filename\\"");

You need to do this to be able to “send” files that have spaces in the filename.

Also, how did you put the PDF in the database?

Thanks Mittineague,
I have tried to include the suggested headers, no luck. The blob was inserted into the db with the following code extract:

--------Code Start---------
$uploadfile = $_FILES[‘upload’][‘tmp_name’];
$uploadname = $_FILES[‘upload’][‘name’];
$uploadtype = $_FILES[‘upload’][‘type’];
$uploadsize = $_FILES[‘upload’][‘size’];
$uploaddesc = $_POST[‘desc’];
$f=fopen($uploadfile,“r”);
$uploaddata=fread($f,filesize($uploadfile));
$uploaddata = addslashes($uploaddata);
include ‘…/Dbcnx.php’;
mysql_query(“insert into docs(Title,DocData,DocType,DocName,DocSize) values (‘$uploaddesc’,‘$uploaddata’,‘$uploadtype’,‘$uploadname’,‘$uploadsize’)”);
------Code End--------
The blob is uploaded into the db as I can view the binary data from the MySQL Query Browser. Also in another part of my web app I can view the pdf by following the same method explained perviously. So, being a newbie, I thought I could transplant this “viewing” feature to the public section to allow users to view these pdfs.

Thanks

ScallioXTX,

All spaces in file names have been replaced with underscores(_) so I’m certain the filename is not the problem. I tried your suggestion anyway but no luck.

Please see previous post for code used to insert pdf into db.

Thanks.

Hello,

I have just found that this viewing function works on IE 6 but not on IE 7. It would appear that the problem is browser related and not php related.

Any idea how I can make this function workable on many if not all browsers?

Thanks for the assistance so far.

Cheers

Hello again,

Just managed to get IE 7 to work, it was a problem with ActiveX for adobe reader. I had to run repair to get it fixed. It is important to ensure that the follwing files are present for adobe reader:

(Typical File location C:\Program Files\Common Files\Adobe\Acrobat\ActiveX)

  • AcroIEHelper.dll
  • AcroPDF.dll (This one was missing on my system)
  • pdfshell.dll

I am now able to view pdf in same window on both IE 6 and IE 7.

Cheers