Logan Besecker
Logan Besecker

Reputation: 2761

images downloading instead of being displayed

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

Upvotes: 0

Views: 3933

Answers (3)

Landin Martens
Landin Martens

Reputation: 3333

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?

Upvotes: 0

user188654
user188654

Reputation:

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;

Upvotes: 3

stewe
stewe

Reputation: 42622

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

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

Upvotes: 4

Related Questions