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

6
  • What is stored in db ? How ? Do you know that blob type is limited to 64k ? Commented May 25, 2009 at 16:48
  • the actual image is stored as blob in the database the rest of the data is varchar. The image shows as [BLOB - 64.0 KiB] in the db Commented May 25, 2009 at 16:50
  • Are the pictures uploaded and stored in your db less than 64K ? Are the image type is a correct mime type like image/png or image/jpeg ? Commented May 25, 2009 at 17:03
  • No the images are more than 64k Commented May 25, 2009 at 17:07
  • Change the type. Upload a new image, and try with it. With JPG compression, if the file is not complete, you'll error while displaying. Commented May 25, 2009 at 17:13

7 Answers 7

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

Sign up to request clarification or add additional context in comments.

11 Comments

I have added them , and also changed it to MEDIUMBLOB but still the same.
The Content-Dispostion header should only be used for downloads. It should not be used for sending the image for a <img> tag.
Hi I have tried this...and it tries to download a file called 4
update - in firefox I get the error The image /view_image.php?id=4” cannot be displayed, because it contains errors.
I fixed some errors in the code. Btw, the filename should be 4.jpeg or even 4.pjpeg Try, var_dump($row); to see what you get from the db.
|
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.

Comments

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

Comments

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?

1 Comment

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?
0

maybe you could try

$row = mysql_fetch_assoc($result);

instead of

$row = mysql_fetch_array($result);

1 Comment

I think by default, mysql_fetch_array will store both the numbers and the names, so both $row[0] and $row['columnname'] are valid
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.

2 Comments

I have done this and re-uploaded an image, now being shown as [BLOB - 369.9 KiB]
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)
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;?>'>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.