Web Design Forums

Welcome! Please register or log in: Forgot your password? Why register?
You are here: Web Design Forums » Programming Help » PHP » SOLVED: Display image stored in MySQL RSS

SOLVED: Display image stored in MySQL

This thread was started by bluefireco and has been viewed 1510 times, and contains 33 replies, with the last reply made by smoseley.
Page 1 of 2: 12 > Next Post Reply
1
View bluefireco's reputation
Posted January 26 '10 at 02:51 AM
      Posts: 21
Hi everyone,

I'm having a slight problem with displaying an image stored in my mysql database. The data is being stored ok, I tested this by outputting the image data to a file. When I went to open the file, there was my image. However, when I go to display the image in the browser I get problems. The following is the code in my image loader file...

<?php
//mysql connect & retrieve stuff would go here

header("Content-Type: image/jpg"); // The image I am loading is in fact a .jpg

//$fp=fopen("test_image.jpg","w+");
//fwrite($fp,$image_data); // this resulting image appears fine
//fclose($fp);

echo $image_data// this one does not
?>

Any ideas what I am doing wrong? Any help is appreciated.

Advertisement Register for free to hide these ads and participate in discussions!

2
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted January 26 '10 at 09:00 AM
      Posts: 3,100
If you're storing your images in MySQL,
what is the fwrite portion for? I'm confused.
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?

3
View bluefireco's reputation
Posted January 26 '10 at 12:03 PM
      Posts: 21
It was the test to make sure the data was being stored properly, which it is. Which is also why it is commented out. I guess I shouldn't have included it.

4
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted January 26 '10 at 12:09 PM
      Posts: 3,100
Show us the script then, where you're displaying the JPEG from your MySQL database.

EDIT:
And I assume your image variable is a BLOB?



.
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?
Last edited January 26 '10 at 12:13 PM by mlseim. Reply

5
View bluefireco's reputation
Posted January 26 '10 at 12:27 PM
      Posts: 21
The code I posted above is most of it, but here's the entire thing. Yes, it is stored as blob, again, the data outputs to a file perfectly. It has problems being output in the browser.

<?php

$mysql_con 
mysql_connect("127.0.0.1","root","***")
mysql_select_db("pic_db");

$result mysql_query("SELECT * FROM pics where id = ".$_GET['id']." LIMIT 1");
$data mysql_fetch_row($result);


// This tells the browser that the content should be a jpg image...
header("Content-Type: image/jpg");

// This is where the data is being given to the browser
echo $data[2];

?>

6
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted January 26 '10 at 12:39 PM
      Posts: 3,100
Let's say that script you show is called "image.php" ...

On another page, you do this:

<html>
<body>
<img src="image.php?id=235" />
</body>
</html>

You're saying that doesn't work?
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?

7
1,325 points at 100% Repute WDFplus Member
Posted January 26 '10 at 12:52 PM
      Posts: 8,434
It should work anyway.

Make sure you have no white space (spaces or return carriages) outside your <? PHP ?> block. That could corrupt the output.

Also, isn't the correct mime type JPEG (with an "E")? so it shoudl be header("Content-Type: image/jpeg");
Steven Moseley
President, Transio

8
1,325 points at 100% Repute WDFplus Member
Posted January 26 '10 at 12:53 PM
      Posts: 8,434
If the above doesn't work, try adding a Content-Length header (just do a strlen() on the image data to get that value).
Steven Moseley
President, Transio

9
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted January 26 '10 at 01:44 PM
      Posts: 3,100
Steven ...

I missed this one: image/jpeg (that might be it).
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?

10
View bluefireco's reputation
Posted January 26 '10 at 06:43 PM
      Posts: 21
Tried those with no luck. I think it may have to do with the way the text is formatted when it shows up on the page. When I just display the raw data, all the new lines disappear. Is there any way to keep that from happening?

On a side note, the reason I had "content-type: image/jpg" is because when I checked the header information for other images (on my own site) it was image/[file ext] depending on the file type.

11
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted January 26 '10 at 07:35 PM
      Posts: 3,100
Show us the actual page ... where the image is supposed to appear.
And also the PHP scripting that creates that page?
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?

12
View bluefireco's reputation
Posted January 26 '10 at 10:25 PM
      Posts: 21
Here's a link to the page,

http://clan.bluefirecode.com/system/..._pic.php?id=28

The script that creates the page is almost exactly what's in my third post. All the page is doing is loading an image to be called later. No html or anything, it's simply an image.

13
View bluefireco's reputation
Posted January 26 '10 at 11:20 PM
      Posts: 21
Ok, I've given up on this storage strategy. Instead I'm going to store references to the image files (as means of security). I've sniffed around the internet for hours now and apparently I'm not the only one with this problem (and still no solution).

Thanks for your help anyway.

14
34 points at 74%
Dorky, Freelance Home page   Private message  
Posted January 27 '10 at 03:12 AM
      Posts: 786
ok dude. your not actually echoing the image like this
$image_data
your just trying to echo the data. this can be done but not with echo. instead use this
http://www.php.net/manual/en/function.image2wbmp.php
if you want to use echo then you will need to echo the image in an image tag and src= to the image file you wrote to.

15
View bluefireco's reputation
Posted January 27 '10 at 07:45 AM
      Posts: 21
Yea, I suppose GD would work, however that's not installed on my server (right now anyway). The problem is that the way I was doing it should work, you don't need the img tag or anything (and believe me I tried that at one point). The idea is that you tell the browser that it's about to display an image, using the headers, and then just output the image data and it should display the image (the equivalent of right-clicking on an image and hitting 'View Image').

16
1,325 points at 100% Repute WDFplus Member
Posted January 27 '10 at 08:42 AM
      Posts: 8,434
Echo works just fine... you're storing the image data as a BLOB in the DB, right? You should be able to just echo it to the page.

GD is used to actually CREATE an image, or to alter an existing image.
Steven Moseley
President, Transio

17
1,325 points at 100% Repute WDFplus Member
Posted January 27 '10 at 08:48 AM
      Posts: 8,434
There's some funky stuff going on in that image. Can you attach the original so I can compare?
Steven Moseley
President, Transio

18
1,325 points at 100% Repute WDFplus Member
Posted January 27 '10 at 08:52 AM
      Posts: 8,434
Can you do me a favor and post the script you're using to insert the image in the DB? I think that may be where the problem is.
Steven Moseley
President, Transio

19
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted January 27 '10 at 09:22 AM
      Posts: 3,100
And also give us the listing for this script: get_pic.php

scratch-out any MySQL login stuff.
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?

20
34 points at 74%
Dorky, Freelance Home page   Private message  
Posted January 27 '10 at 10:06 AM
      Posts: 786
or imagejpg(); i have had the same issue, like you said, as have others. i just think there are too many other ways to do it then butting you head on a buggy approach.

21
34 points at 74%
Dorky, Freelance Home page   Private message  
Posted January 27 '10 at 10:36 AM
      Posts: 786
are the other images on the page or is this being tested as standalone?

22
View bluefireco's reputation
Posted January 27 '10 at 05:35 PM
      Posts: 21
Link to image we're trying to display:
http://clan.bluefirecode.com/system/...aded_image.jpg
Link to the image loader page (get_pic.php?id=28):
http://clan.bluefirecode.com/system/..._pic.php?id=28
...and it's contents:
<?php

$mysql_con 
mysql_connect("127.0.0.1","root","***")
mysql_select_db("pic_db");

$result mysql_query("SELECT * FROM mypic_pics WHERE id = ".$_GET['id']." LIMIT 1");
$data mysql_fetch_row($result);

$image $data[2]; // data[2] is the image contents

header("Content-Length: ".strlen($image));
header("Content-Type: image/".$data[3]); //data[3] is the file type (jpg/jpeg/png/gif)

echo $image;
?>

here is the upload script...

// these two lines are specifically for finding the file extension if the name has more than one '.' in it.
$name_array explode(".",$_FILES['picture_upload']['name']);
$last_index sizeof($name_array) - 1

$image_data file_get_contents($_FILES['picture_upload']['tmp_name']);    
$image_data mysql_real_escape_string($image_data);

$query "INSERT INTO mypic_pics (user,data,ext,caption,password,album,public) VALUES ('admin','";
$query .= $image_data."','".$name_array[$last_index]."','A test caption.',' ','0','0')";

$result mysql_query($query); 


My image data is being stored as a long blog, which I can't imagine is the problem.

Again, I don't believe that the storage is the problem because I can pull the data out of mysql and write it to a normal jpg file just fine; the image appears without error. The error occurs when I try to give the data directly to the browser.

If you want to try your own upload, I'm having the script tell you your id (it wil appear right above 'home'... here is the link to the page:

http://clan.bluefirecode.com/page/?page=home

23
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted January 27 '10 at 06:48 PM
      Posts: 3,100
How do you know that $data[2] is the image contents?
$image = $data[2]; // data[2] is the image contents

I would expect it be like this ....

Say for instance, that the name of your MySQL image field is 'data' ...

$image=$data['data'];
$extension=$data['ext'];

You didn't give us the names of your columns, but I think you should reference the name of the column.

EDIT ... yes, you showed us the names .. use 'data' instead of 2.
also the other ones.
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?
Last edited January 27 '10 at 06:52 PM by mlseim. Reply

24
View bluefireco's reputation
Posted January 27 '10 at 09:34 PM
      Posts: 21
How do you know that $data[2] is the image contents?
$image = $data[2]; // data[2] is the image contents

I would expect it be like this ....

Say for instance, that the name of your MySQL image field is 'data' ...

$image=$data['data'];
$extension=$data['ext'];
Yea, sorry, id is set to auto-increment and was not included in sql script. Here are all the columns...

id,user,data,ext,caption,password,album,public

Rest assured that $data[2] is in fact the data (I've checked it's output to a file). But thanks for the advice for naming, probably much easier than using the numbers.

25
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted January 27 '10 at 10:05 PM
      Posts: 3,100
What happens if you do this (manually force a jpeg content-type) ... since I have no way to test it myself ...

save your original in a safe place and try this in it's place ...
<?php

$mysql_con 
mysql_connect("127.0.0.1","root","***")
mysql_select_db("pic_db");

$result mysql_query("SELECT * FROM mypic_pics WHERE id = ".$_GET['id']." LIMIT 1");
$data mysql_fetch_row($result);

$image $data['data'];

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

echo 
$image;
?>
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?

Page 1 of 2: 12 > Next Post Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to insert image to mysql ketanco PHP 4 June 18 '09 02:56 AM
Why won't Explorer display this image? johnjm22 Adobe Photoshop Help 9 September 9 '06 02:08 AM
"Image loading" graphic won't display, and one other image gallery problem TheGardener Javascript, AJAX, and JSON 1 July 10 '06 09:06 AM
Random Image Display network traffic with IE6 yardbird Javascript, AJAX, and JSON 0 May 30 '05 12:58 AM