Forum Moderators: coopster

Message Too Old, No Replies

Displaying Images from MySQL BLOB

         

nolim8ts

1:46 pm on Jan 31, 2010 (gmt 0)

10+ Year Member



Hi All,

I'm using the code below to display an image which is being stored as a BLOB in MySQL. When trying to display the image, all that is displayed is a red cross in IE. I looked at the BLOB data via phpMyAdmin and this is some of the data that is being stored. Any help would be appreciated...

"GIF89a\x0\x4\x0\x3χ\x0\x0\x4\x2\x4\x11ˆ\x11›‹5ΟΝ%ˆ\x3\x4‰‹“‘Ι‘

 
<?php

/*** some basic sanity checks ***/
if(filter_has_var(INPUT_GET, "image_id") !== false && filter_input(INPUT_GET, 'image_id', FILTER_VALIDATE_INT) !== false)
{
/*** assign the image id ***/
$image_id = filter_input(INPUT_GET, "image_id", FILTER_SANITIZE_NUMBER_INT);
try {
/*** connect to the database ***/
$dbh = new PDO("mysql:host=localhost;dbname=db", 'username', 'password');

/*** set the PDO error mode to exception ***/
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

/*** The sql statement ***/
$sql = "SELECT image, image_type FROM tbl_photos WHERE image_id=$image_id";

/*** prepare the sql ***/
$stmt = $dbh->prepare($sql);

/*** exceute the query ***/
$stmt->execute();

/*** set the fetch mode to associative array ***/
$stmt->setFetchMode(PDO::FETCH_ASSOC);

/*** set the header for the image ***/
$array = $stmt->fetch();

/*** check we have a single image and type ***/
if(sizeof($array) == 2)
{
/*** set the headers and display the image ***/
header("Content-type: " .$array['image_type']);

/*** output the image ***/
echo $array['image'];
}
else
{
throw new Exception("Out of bounds Error");
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
catch(Exception $e)
{
echo $e->getMessage();
}
}
else
{
echo 'The Photo ID supplied is not valid';
}
?>

Matthew1980

8:14 pm on Jan 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Nolim8ts,

Welcome to webmaster world.

Just noticed something in your code, whether it makes a huge difference or not, but it might be worth changing:


/*** set the headers and display the image ***/
header("Content-type: " .$array['image_type']);

try this:-


/*** set the headers and display the image ***/
header("Content-type: image/" .$array['image_type']);

Only reason I point this out is because in the content type you need to specify that its an image, and then specify what type of image, as I say give it a try - hopefully it will prove successful.

Cheers,

MRb

nolim8ts

11:45 pm on Jan 31, 2010 (gmt 0)

10+ Year Member



Hi MRb,

Thanks for the reply. I tried your solution and now the debugger is showing Error: E_Warning. Cannot modify header information - headers already sent out.

the value being stored in .$array['image_type'] = image/gif

Update: The issue has been resolved. It was because I had a space between the opening PHP tag and IF Statement. Thanks MRb. Without your suggestion, I would have never worked out what was going wrong...

Matthew1980

8:21 am on Feb 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Nolim8ts,

Nice to hear that it has been resolved.

Glad to help anyway.

Cheers,

MRb