1

Hi I have a image table in my database. These are stored as blob along with details such as image type & name.

I am having a problem showing the image, all I get is a white box with a red cross in it. code:

<?php

include '../connection.php';

$ID = $_GET['id'];

$query = "SELECT * FROM `images` WHERE `image_id` = '$ID'";

$result=mysql_query($query);
$row = mysql_fetch_array($result);

$image = $row['image'];
$image_type= $row['image_type'];   

header("Content-type: $image_type");
print $image; 

exit;

?>

Thanks

Elliott
  • 3,812
  • 24
  • 69
  • 93

7 Answers7

3

Well here is a short answer.

<?php
include '../connection.php';
$id = (int)$_GET['id'];
$query = "SELECT * FROM `images` WHERE `image_id` = '$id'";

$result=mysql_query($query);
$row = mysql_fetch_array($result);

$image = $row['image'];
$image_type= $row['image_type'];
$size = $row['image_size'];
//alternative
/* list($image, $image_type, $size) = array(
                                       $row['image'],
                                       $row['image_type'],
                                       $row['image_size']
                                      );
*/
$ext = explode('/', $image_type);
$name = $id . '.' . $ext[1]; 

header("Content-type: $image_type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");

print $image;     
exit;

Check your blobtype to be a least MEDIUMBLOB which is able to store data up to 16M

Boris Guéry
  • 47,316
  • 8
  • 52
  • 87
1

To debug this, I'd suggest commenting-out the Content-type header line, then hitting the url directly in the browser. This will allow you to see any errors, warnings or notices that PHP might be emitting.

Jon Benedicto
  • 10,492
  • 3
  • 28
  • 30
0

Or if you don't want to create a separate php file, you can inline it

<?php
// retrieve blob into $img
?><img src='data:image/png;base64,<?php echo base64_encode( $img );?>' alt='Image <?php echo $id;?>'>
Jeff
  • 2,095
  • 25
  • 18
0

That looks like it might work, what's going wrong?

Try to specify the fields explicitly:

 SELECT image, image_type FROM ...

What happens when you run the query from the database?

Are you loading the image like:

<img src="image.php?id=12">

Or do you load the PHP as its own page?

Andomar
  • 232,371
  • 49
  • 380
  • 404
  • I load the image on its own page such as view_image.php?id=2 I get no errors from the query but running in Firefox I get the url shown in the page. but in IE I get a pic box with red cross? – Elliott May 25 '09 at 17:00
0

Couple of things to try

  1. maybe something is failing and it is returning an error msg in html instead of the expected image

  2. is the content_type stored correctly in the database or are you just storing the file extension of the image

content-type should look something like this

image/gif 
image/jpeg
bumperbox
  • 10,166
  • 6
  • 43
  • 66
0

maybe you could try

$row = mysql_fetch_assoc($result);

instead of

$row = mysql_fetch_array($result);
bumperbox
  • 10,166
  • 6
  • 43
  • 66
  • I think by default, mysql_fetch_array will store both the numbers and the names, so both $row[0] and $row['columnname'] are valid – Andomar May 25 '09 at 17:05
0

You said in a comment that the table initially said "[BLOB - 64.0 KiB]" but you then changed it to "MEDIUMBLOB". This will expand the size that you can store, but all of your existing data will still be truncated to 64KiB.

Make sure that the field type you use is large enough to store the data you want to store (16mb in a MEDIUMBLOB or ~4gb in a LONGBLOB I'm pretty sure) and then re-insert all of your data.

Other than the security problems mentioned, I don't see why the code shouldn't work other than the database problem.

Drakia
  • 279
  • 1
  • 3
  • I have done this and re-uploaded an image, now being shown as [BLOB - 369.9 KiB] – Elliott May 25 '09 at 17:45
  • Does it show properly on the page when you visit it now? (Using the code in your original question). I tried that exact code out and it seemed to work fine (Using a jpeg, not a pjpeg though) – Drakia May 25 '09 at 19:08