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.
<?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; ?>
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
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; 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
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.
Couple of things to try
- maybe something is failing and it is returning an error msg in html instead of the expected image
- 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
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:
Or do you load the PHP as its own page?
maybe you could try
$row = mysql_fetch_assoc($result);
$row = mysql_fetch_array($result);
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.
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;?>'>