繁体   English   中英

无法显示PHP中数据库中的图像

[英]Cannot display Image from database in php

我正在使用smarty,mysql,而我只是试图显示图像。 这是我得到的错误-

资源被解释为图像,但以MIME类型text / html传输

我的index.php文件

<img src="http://localhost/admin/image2.php?id={$editWine[0].id}" width="150" height="260" border="0" class="bottle-img" id="smallImageWineEdit" />

我的image2.php文件

$id=$_REQUEST['id'];        
$sql="SELECT * FROM table WHERE id=$id";        
$result=mysql_query($sql) or die(mysql_error());
while($row=@mysql_fetch_assoc($result))
{
   echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image_data'] ).'" width="150" height="150" /> &nbsp';  
   echo $row['image_data'];                     
}

while循环内的此回声工作正常。

当我检查元素并在新选项卡中打开此img链接时,将显示图像。 而它不会显示在当前页面中。

问题是您再次从图像文件传递了html标签。 您只需要输出具有适当图像内容类型的图像数据即可。

像编辑image2.php

if($row=@mysql_fetch_assoc($result))
{
   header('Content-type: image/jpg');
   echo $row['image_data'];
   exit();                   
}

更新至以下评论

进行以上更改,并在浏览器中请求http://localhost/admin/image2.php?id=VALID_ID_HERE并检查其是否重新调整了图像。 然后,可以在index.php的src标记中使用它以在此处显示它。 如果在渲染图像时遇到错误,请在请求图像时确保在数据库中请求正确的图像ID,并使用所看到的错误消息更新问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM