tags:

views:

860

answers:

7

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

A: 

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
I load the image on its own pagesuch as view_image.php?id=2I 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
Try to remove the "exit" ?
Andomar
A: 

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
I have added them , and also changed it to MEDIUMBLOB but still the same.
Elliott
The Content-Dispostion header should only be used for downloads. It should not be used for sending the image for a <img> tag.
Jon Benedicto
I removed that..as in FF it tried to download the file.
Elliott
Looking at his code, he doesn't like he wants to use the <img /> tag...
Boris Guéry
Hi I have tried this...and it tries to download a file called 4
Elliott
update - in firefox I get the errorThe image /view_image.php?id=4” cannot be displayed, because it contains errors.
Elliott
I fixed some errors in the code.Btw, the filename should be 4.jpeg or even 4.pjpegTry, var_dump($row); to see what you get from the db.
Boris Guéry
Thanks I now get:<br /><b>Warning</b>: filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for 4.pjpeg in <b>/home/""/public_html/""/images/view.php</b> on line <b>13</b><Plus alot of un-readable coding.
Elliott
tested in firefox..it downloads file called 4.pjpegbut cannot be opened
Elliott
humm mea culpa, filesize is used to check a the file size.You should store the size of the file when uploading it, you should be able to this by using the function above on the temp file.
Boris Guéry
I store the filesize when uploadingthis is in the row image_size
Elliott
So replace, $row['size'] by $row['image_size'] in the code
Boris Guéry
I did this and the page tried to download 4.pjepg as a html doc.
Elliott
What do you mean ?
Boris Guéry
it tried to download the jpeg image...but it was shown as a pjpeg file type, which could not me opended.
Elliott
A: 

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
image type in the db is shown as " image/pjpeg "
Elliott
A: 

maybe you could try

$row = mysql_fetch_assoc($result);

instead of

$row = mysql_fetch_array($result);
bumperbox
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
you learn something new every day :)
bumperbox
+1  A: 

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
+2  A: 

Never, never ever use code like that again:

$ID = $_GET['id'];

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

Google "sql code injection" and you will see why. This kind of code is actually often an anti-example.

Flavius Stef
I understand that...this isn't a complete script yet. I usually use stripslashes or some other form to prevent this. Thanks anyway
Elliott
addslashes/stripslashes is not enough. Read http://stackoverflow.com/questions/534742/what-does-mysqlrealescapestring-do-that-addslashes-doesnt
Flavius Stef
Will look into it, ta
Elliott
A: 

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
I have done this and re-uploaded an image, now being shown as [BLOB - 369.9 KiB]
Elliott
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
Thanks, but no it doesnt work.
Elliott