Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
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

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

macrumors newbie
Jul 28, 2008
4
0
Un/Serialize

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

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
You've got two header directives active

Code:
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

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
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)..
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.