2012-04-15 106 views
0

我将图像作为mediumblobs存储在mySQL服务器上。当我尝试用下面的代码显示它们时,一些浏览器(如safari和EI)下载图像而不是显示它们。有没有办法显示它们是独立于浏览器的?下载图片而不是显示

$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; 

在此先感谢

+0

有一个读:http://stackoverflow.com/questions/5932249/show-a-blob-image-php-mysql-along-with-other-data,http://stackoverflow.com/ question/5525830/displays-an-image-stored-in-a-mysql-blob,http://stackoverflow.com/questions/1760754/how-to-display-an-image-from-a-mysql-blob – bostaf 2012-04-15 02:14:39

回答

4

尝试Content-Type: image/jpeg代替Content-Type: image/jpg

image/jpeg是正确的MIME类型为JPEG图像。

0

试试这个

$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; 

$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>"; 

还要注意,一些浏览器做的,如果没有HTML内容下载的图像。你尝试过其他浏览器吗?

2

尝试发送一个Content-Disposition标头,其值inline像这样。

$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;