[nycphp-talk] Displaying images from a database
Daniel Krook
krook at us.ibm.com
Sat Apr 23 00:36:14 EDT 2005
Hello folks,
I'm trying to display image content that has been stored in a DB2 v8.2
database as a BLOB along with its filename, size, and content type which
was passed along when they were inserted via a form upload. Storing links
to physical files outside the database is not an option for this
application.
I know that the data that is stored is not corrupt because the non-PHP
content management tool that works with the uploads can redisplay them
properly. It's displaying them in PHP that is the issue. I'm able to
get the values I need from the database into a Photo object correctly with
the following function. [The FOR READ ONLY bit is a solution for getting
BLOB data through ODBC correctly in PHP].
public function getCatPhoto ($catId) {
global $db;
$retPhoto = new Photo();
$sql = 'SELECT NAME, SIZE, TYPE, DATA
FROM DB2INST1.PHOTOS P, DB2INST1.CAT_PHOTO CP
WHERE CP.CAT_ID = ?
AND CP.PHOTO_ID = P.PHOTO_ID FOR READ ONLY';
$res =& $db->query($sql, (int)$catId);
if (PEAR::isError($res)) {
die($res->getMessage());
} else {
if ($row =& $res->fetchRow()) {
$retPhoto->name = $row['NAME'];
$retPhoto->size = $row['SIZE'];
$retPhoto->type = $row['TYPE'];
$retPhoto->data = $row['DATA'];
}
}
$res->free();
return $retPhoto;
}
I then take this Photo object and try to display it in the browser. Note
that there's no whitespace anywhere that's messing with the headers. I
have experimented with quite a few cache control headers to no avail.
Firefox and IE on the PC and Safari on the Mac all fail to display the
image. IE gives me the stream of data as it looks in the database.
Firefox sometimes displays the data and other times tells me that "The
image "x" cannot be displayed, because it contains errors." I've also
tried unsuccessfully using force download techniques with
header('Content-Disposition: attachment; filename="' . $photo->name .
'"');
$photo = getCatPhoto($_GET['catId']);
header('Content-Type: ' . $photo->type);
header('Content-Length: ' . $photo->size);
echo $photo->data;
The script can can be tested here:
http://catabase.krook.org/inc/util/view-photo.php?catId=34 (JPEG)
http://catabase.krook.org/inc/util/view-photo.php?catId=60 (GIF)
Can anyone see something that I'm missing? Everything I've Googled points
to examples that state that at a minimum the case-sensitive Content-Type
header must be there and to use the echo function (as opposed to print
because it's not so picky about unstructured data). Are there any other
things I'm overlooking?
Thanks in advance.
Daniel Krook, Advisory IT Specialist
Application Development, Production Services - Tools, ibm.com
Personal: http://info.krook.org/
BluePages: http://w3.ibm.com/bluepages?searchcnum=9A9796897
More information about the talk
mailing list