Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem showing PDF file from a MySQL blob field

Newbie
 
Join Date: Aug 2007
Posts: 26
#1: Aug 17 '07
I'm having a problem reading a pdf from a longblob mysql field.
My code works in safari and firefox, but IE6 gives an error of (file could not be written to cache) and IE7 just gives a blank page. This is my code:

[PHP]<?php
if (isset($_GET['fileID']) == false) { header("Location: studentsindex.php"); exit;}
if ($_GET['referrer'] == "papers") { $table = "tbl_pastpapers"; } else {$table = "tbl_subjectmaterial"; }
mysql_select_db($database_conn_LSM, $conn_LSM);
$result=mysql_query("SELECT fileID, filepdf FROM ".$table." WHERE fileID='".$_GET['fileID']."'");
$row=mysql_fetch_assoc($result);
Header( "Content-type: application/pdf");
echo $row['filepdf'];
?>[/PHP]

Any suggestions as to how to adapt this to IE?

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#2: Aug 18 '07

re: Problem showing PDF file from a MySQL blob field


I have moved your post into it's own thread in the PHP Forum, where it is more likely to be answered.
Please ask new questions in new threads, rather than posting them in an old one.

Moderator
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#3: Aug 18 '07

re: Problem showing PDF file from a MySQL blob field


Quote:

Originally Posted by erp23

I'm having a problem reading a pdf from a longblob mysql field.
My code works in safari and firefox, but IE6 gives an error of (file could not be written to cache) and IE7 just gives a blank page. This is my code:

[PHP]<?php
if (isset($_GET['fileID']) == false) { header("Location: studentsindex.php"); exit;}
if ($_GET['referrer'] == "papers") { $table = "tbl_pastpapers"; } else {$table = "tbl_subjectmaterial"; }
mysql_select_db($database_conn_LSM, $conn_LSM);
$result=mysql_query("SELECT fileID, filepdf FROM ".$table." WHERE fileID='".$_GET['fileID']."'");
$row=mysql_fetch_assoc($result);
Header( "Content-type: application/pdf");
echo $row['filepdf'];
?>[/PHP]

Any suggestions as to how to adapt this to IE?

As PHP is executed server-side, the PHP code itself is not the problem, the output you are sending to the browser is. My guess would be that there is a problem with the headers.
The 'application/pdf' header is recognized by IE as a Acrobat Reader document, but it may require that you send the length of the document.

Try sending the 'Content-length' header and see if that helps.
Expand|Select|Wrap|Line Numbers
  1. header("Content-length: ". strlen($row['filepdf']) )";
  2.  
You may also want to send the PDF document name to the browser, which would be done like this:
Expand|Select|Wrap|Line Numbers
  1. header("Content-Disposition: inline; filename=docname.pdf");
  2.  
Newbie
 
Join Date: Aug 2007
Posts: 26
#4: Aug 18 '07

re: Problem showing PDF file from a MySQL blob field


I've tried both header lines without success - I still get the cannot write to cache error in IE.
I'm not sure whether the content-disposition filename seems to work, since changing tis to attachement gives me the name of the page rather than the filename with a pdf extension. My code so far is:
[PHP]<?php
if (isset($_GET['fileID']) == false) { header("Location: studentsindex.php"); exit;}
if ($_GET['referrer'] == "papers") { $table = "tbl_pastpapers"; } else {$table = "tbl_subjectmaterial"; }
mysql_select_db($database_conn_LSM, $conn_LSM);
$result=mysql_query("SELECT fileID, filepdf FROM ".$table." WHERE fileID='".$_GET['fileID']."'");
$row=mysql_fetch_assoc($result);
header( "Content-type: application/pdf");
header( "Content-length: ".strlen($row['filepdf'])."");
header( "Content-Disposition: attachment; filename=subject.pdf");
echo $row['filepdf'];
?>[/PHP]

Any suggestions? Would a different display method work (e.g. readfile??) and if so, how would I do it?
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#5: Aug 18 '07

re: Problem showing PDF file from a MySQL blob field


Heya, ERP.

Is your internet cache set too small? Check your internet settings.
Are you running a secure server (https://)?

Can you load the file in Firefox?
Newbie
 
Join Date: Aug 2007
Posts: 26
#6: Aug 18 '07

re: Problem showing PDF file from a MySQL blob field


Yes, the file uploads without a problem in Firefox, Safari, Navigator and Opera.

I think you have located the problem - I have been using https for all the members pages. If I disable this, the pdf download works fine in IE. Is there a way around this?
Newbie
 
Join Date: Aug 2007
Posts: 26
#7: Aug 18 '07

re: Problem showing PDF file from a MySQL blob field


I've added the following two snippets which seem to have fixed the problem...

[PHP]header( "Pragma: private");
header( "Cache-Control: max-age=0"); [/PHP]

is that a reasonable solution?
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#8: Aug 18 '07

re: Problem showing PDF file from a MySQL blob field


Heya, erp.

This is a problem with Internet Explorer, not your code.

Have a look at this article.
Newbie
 
Join Date: Aug 2007
Posts: 26
#9: Aug 18 '07

re: Problem showing PDF file from a MySQL blob field


i know - although they say this is only for SP1, the bug is still there in IE7, so the cache corrections will probably still be necessary...
Reply