Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Why can't I display a image stored as blob from MySQL in PHP ?

Posted on 2003-03-20
11
Medium Priority
?
467 Views
Last Modified: 2012-08-13
I have a "image" table structure like this :
(image_id int
image_type varchar(50)
image blob
image_size bigint
image_name varchar(255)
image_date datetime)

and I've a image.php scripted as :
<html>
<head>
<title>My Image</title>
</head>
<body>
<H1>Look at this:</H1>
<IMG SRC="load_image.php">
</body>
</html>

And in the load_image.php, here's the script:
<?
$db = @mysql_connect(localhost, "root", "root") or die("Can't connect to server.");

@mysql_select_db("dating", $db) or die("Can't select database.");

//Create and execute the SQL statement:

$get_image = "select image, image_type from image where image_id = 8";

$get_image_result = @mysql_query($get_image)  or die("Couldn't get image.");

//Get the individual elements out of the result set:

$binary_junk = @mysql_result ($get_image_result,0,"image");

$filetype = @mysql_result ($get_image_result,0,"image_type");

header("Content-type: $filetype");
echo "$binary_junk";
?>

WHEN THE image.php IS LOAD IN BROWSER, THE IMAGE COULD NOT DISPLAYED, WHY ? I?M SURE THE BLOB FIELD IS FILLED UP WITH CORRECT DATA. CAN ANYBODY TELL ME WHAT's GOING WRONG OR WHERE CAN I FIND A SCRIPT THAT INSERT IMAGE AND SELECT IMAGE THAT WORKS.
0
Comment
Question by:edmondlim
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 6

Expert Comment

by:CyberGhost
ID: 8172597
Actually I don't understand why are you storing image in BLOB database. You could just put it on server and then load it from there. Why getting it from DB?

But this code will maybe help you a little:

$result = mysql_query("SELECT image, image_type from image where image_id = 8");

while ($row = mysql_fetch_array($result))
{
 $binary_junk = $row["image"];
 $filetype = $row["image_type"];
}
mysql_free_result($result);

header("Content-type: $filetype");
echo $binary_junk;


... that is a well-known statement that works (as far as I know). Or check if you have good field names written and can connect to DB (there is no OR DIE statement when connecting to DB in your code). These are the most frequent mistakes people do working with databases.

regards
0
 
LVL 6

Expert Comment

by:CyberGhost
ID: 8172616
... sorry, my mistake. I have missed that u have OR DIE statement in there. Well... just check your table field names or try the code above...
0
 

Author Comment

by:edmondlim
ID: 8172659
Sorry, it still doesn't display the image. As far as I can see, your different is just that putting the contents in array.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

by:CyberGhost
ID: 8172706
Well... then the problem is maybe not in the PHP code.


Maybe you can try to use this:

<html>
<head>
<title>My Image</title>
</head>
<body>
<H1>Look at this:</H1>
<IMG SRC="<?php include("load_image.php"); ?>">
</body>
</html>

... instead of using that page as a SRC parameter.
0
 
LVL 6

Expert Comment

by:CyberGhost
ID: 8172782
... anyway there is something I've missed again. You are trying to put the header informations in BODY part, where no headers can be set. These must be set before generating any HTML code. So you maybe could use that code included on the top of your page and then just read the value of $binary_junk right as IMG SRC parameter. But that HTML have to be PHP of course.
0
 
LVL 2

Expert Comment

by:foreverfresh
ID: 8172856
hi,

I don't know what is your uploader code.
please try yhis files

//---------------------------
// upload.php
<html>
<body>
<?
  if(strlen($img_name))
  {
    $SQLdbi = mysql_connect("localhost","root","");
    mysql_select_db("Mydb", $SQLdbi);    
    $imgsize = filesize($img);
    $imgdata = addslashes(fread(fopen($img,"rb"), $imgsize));
    $SQLquery  ="INSERT INTO image (id,image_type,image,image_size,image_name,image_date)"
              ."VALUES ('','$img_type','$imgdata','$imgsize','$img_name',"
              ."'".date("Y-m-d H:i:s")."')";
    mysql_query($SQLquery,$SQLdbi);
  }
?>
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input name="img" type="file" id="img"> <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
//--------------------------------------------

//--------------------------------------------
// showimage.php
<?
  $SQLdbi = mysql_connect("localhost","root","");
  mysql_select_db("Mydb", $SQLdbi);
  $SQLquery  ="SELECT * FROM image WHERE id='1'";
  $result= mysql_query($SQLquery,$SQLdbi);
  $row=mysql_fetch_array($result);
  header("Content-type: $row[image_type]");
  echo "$row[image]";
  mysql_close($SQLdbi);
?>
//---------------------
0
 

Author Comment

by:edmondlim
ID: 8173618
"foreverfresh", your solution doesn't work either. the upload.php managed to store the image uploaded but the showimage.php shows nothing. I wonder is there anything I've to configure in the php.ini file or in the Apache server ?
0
 
LVL 2

Accepted Solution

by:
foreverfresh earned 300 total points
ID: 8173884
did you change id='1' to id=??? in showimage.php
//-------
 $SQLquery  ="SELECT * FROM image WHERE id='1'";
//-------
  or you can use
//
  $SQLquery  ="SELECT * FROM image WHERE id='$imid'";

http://yourserver/showimage.php?imid=5

this code works for me :)

0
 
LVL 2

Expert Comment

by:bobsledbob
ID: 8175135

edmondlim, two thoughts:

1)  just to be sure your 'filetype' field is actually a mime type?  ie. you're storing image/gif in there instead of just gif?  Sending the header Content-type: gif  won't help things out.  

2)  There's some issues around progressive jpegs (mime type image/pjpeg).  You shouldn't have a problem with a newer browser I'm guessing, but for older browsers sending mime type image/jpeg whilst really serving a pjpeg will break.  The opposite is true too.  Like I said though, I think most browswers these days can mix and match progressive and normal jpeg files, regardless of the mime-type the webserver sends them.  Also, sending image/jpg is bad news too, which I've seen some code before do.
0
 

Expert Comment

by:grantwong
ID: 8178068
edmondlim,

What happens if you go to http://yoursever.com/load_image.php

You should see an image.  If you don't, you should be able to diagnose the error from there.  Generally when something like this doesn't work out it is due to some output that is happening.

To ensure that no output is happening before your call to header(), and your echo of the blob, you can do:

ob_end_clean();
ob_start();

to clear the output buffer.

After you echo, you should do a return; or exit;

---
ob_end_clean();
ob_start();
header($filetype);
echo $image;
return;

Hope this helps.

0
 

Author Comment

by:edmondlim
ID: 8179757
It's works now
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question