0

I am storing images on a mySQL server as mediumblobs. When I try to display them with the following code, some browsers(such as safari and EI) download the image instead of displaying them. Is there a way to display them which is browser independent?

$query = "SELECT image FROM images WHERE id=?";
$stmt = $dbc->prepare($query);
$stmt->bind_param("i",$id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($image);
$stmt->fetch();
header("Content-Type: image/jpg");
echo $image;

Thanks in advance

hakre
  • 193,403
  • 52
  • 435
  • 836
Logan Besecker
  • 2,733
  • 4
  • 23
  • 21
  • Have a read: http://stackoverflow.com/questions/5932249/show-a-blob-image-php-mysql-along-with-other-data, http://stackoverflow.com/questions/5525830/displaying-an-image-stored-in-a-mysql-blob, http://stackoverflow.com/questions/1760754/how-to-display-an-image-from-a-mysql-blob – Taz Apr 15 '12 at 02:14

3 Answers3

4

Try Content-Type: image/jpeg instead of Content-Type: image/jpg

image/jpeg is the correct MIME-Type for jpeg images.

stewe
  • 41,820
  • 13
  • 79
  • 75
3

Try sending a Content-Disposition header with the value inline like this.

$query = "SELECT image FROM images WHERE id=?";
$stmt = $dbc->prepare($query);
$stmt->bind_param("i",$id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($image);
$stmt->fetch();
header("Content-Type: text/html");
header("Content-Disposition: inline");
echo $image;
0

try this

$query = "SELECT image FROM images WHERE id=?";
$stmt = $dbc->prepare($query);
$stmt->bind_param("i",$id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($image);
$stmt->fetch();
header("Content-Type: text/html");
echo $image;

or

$query = "SELECT image FROM images WHERE id=?";
$stmt = $dbc->prepare($query);
$stmt->bind_param("i",$id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($image);
$stmt->fetch();
header("Content-Type: image/jpg");
echo "<p>".$image."</p>";

Also note, some browsers do download an image if there is not HTML content. have you tried other browsers?

Landin Martens
  • 3,283
  • 12
  • 43
  • 61