Reputation: 2761
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
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
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
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