PDA

View Full Version : php, mysql, images and BLOB problems




Quboid
Jul 28, 2008, 03:34 AM
Hello everyone,

I am building a web application for my school (I am learning php as i go along) and I am storing some images into a DB table as BLOBs. I am trying to retrieve them now, and I have a script that seems to work but for one problem
when i echo the variable where i store the blob it returns the binary data even when I add the header needed that should return the image, i get a bunch of weird code on the page.

I am aware that storing the images in a file system is more efficient, But I just wanted to see this work.


thanks in advance...


heres the code



<?php

mysql_connect(localhost,root,root);

mysql_select_db(upload);




//echo "hello";


// I am just getting the first table row to try it out


$query="SELECT * FROM upload";

/*$query = "SELECT name, type, size, content ".
******** "FROM upload WHERE id = '41'";*/

$result = mysql_query($query) or die('Error, query failed');

$row = mysql_fetch_array($result);
$content=$row['content'];
$name=$row['name'];

//header("Content-length: $size");

//header("Content-Disposition: attachment; filename=$name");
header("Content-type: image/jpeg");
print $content;


exit;
?>



EliseVanLooij
Jul 28, 2008, 05:28 AM
I'm not current on blobs, but if I'm not mistaken you needed to serialize the image object when you stored it in the database, so it stands to reason you'll need to unserialize it when you retrieve it. You'll then have an image object, from which you can construct an HTML img tag.

lazydog
Jul 28, 2008, 07:15 AM
You've got two header directives active


header('Content-type:*image/jpg');
...
header("Content-type: image/jpg");



Also I think mime type should be image/jpeg. I don't know if that will fix your problem though.

b e n

Quboid
Jul 28, 2008, 12:27 PM
I removed one header,but it still doesn't work, Could it be the way I am uploading the BLOB to the table? It reads back everything else fine (id, name, etc)..

lazydog
Jul 28, 2008, 04:16 PM
Which header did you keep? It should be:-

header("Content-type: image/jpeg");

b e n

jeremy.king
Jul 29, 2008, 11:57 AM
Could it be the way I am uploading the BLOB to the table?

Yes it could.

Here's a full example - http://www.anyexample.com/programming/php/php_mysql_example__image_gallery_(blob_storage).xml

Note these two lines on upload

$data = file_get_contents($_FILES['photo']['tmp_name']);
$data = mysql_real_escape_string($data);