This question already has an answer here:
这个问题在这里已有答案:
- How to retrieve images from MySQL database and display in an html tag 5 answers
- 如何从MySQL数据库中检索图像并在html标签中显示5个答案
I'm attempting to display an image stored in the BLOB column in the database;
我正在尝试显示存储在数据库中BLOB列中的图像;
I fetch the data from the database with a SELECT perform no transformations on the data and display it with the following (from a script whose only output is the following):
我从数据库中获取数据,使用SELECT对数据执行无变换,并使用以下内容显示它(来自脚本,其唯一输出如下):
header("Content-Type: image/jpeg");
echo $image;
Please note chrome is displaying the content size as the correct size for the image as well as the correct mime type (image/jpeg
). nothing is echoing out before the header and ive checked the blob in the database is correct. There is also no trailing whitespace before or after the <?php ?>
tags.
请注意,chrome将内容大小显示为图像的正确大小以及正确的mime类型(image / jpeg)。在标题之前没有任何回声,我已经检查了数据库中的blob是否正确。在<?php?>标记之前或之后也没有尾随空格。
chrome/IE displays an image icon but not the image itself. any ideas?
chrome / IE显示图像图标但不显示图像本身。有任何想法吗?
EDIT: image is got the from the database as such:
编辑:图像是从数据库中得到的:
$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$row = $sth->fetch();
$image = $row['image'];
var_dump($image) gives:
var_dump($ image)给出:
string 'ÿØÿà�JFIF��x�x��ÿá�ZExif��MM�*�����������J��������Q�������Q������tQ������t�����† ��±ÿÛ�C�
ÿÛ�CÿÀ�_"�ÿÄ�����������
ÿÄ�µ���}�!1AQa"q2‘¡#B±ÁRÑð$3br‚
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³ ´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ��������'... (length=60766)
3 个解决方案
#1
91
Try Like this.
试试这样。
For Inserting into DB
用于插入DB
$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$image = addslashes(file_get_contents($_FILES['images']['tmp_name']));
//you keep your column name setting for insertion. I keep image type Blob.
$query = "INSERT INTO products (id,image) VALUES('','$image')";
$qry = mysqli_query($db, $query);
For Accessing image From Blob
用于从Blob访问图像
$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';
Hope It will help you.
希望它会帮助你。
Thanks.
谢谢。
#2
11
This is what I use to display images from blob:
这是我用来显示blob图像的方法:
echo '<img src="data:image/jpeg;base64,'.base64_encode($image->load()) .'" />';
#3
0
Since I have to store varius types of content in my blob field/column, suppose to update my code like this:
因为我必须在我的blob字段/列中存储varius类型的内容,所以假设更新我的代码,如下所示:
echo "data: $mime" $result['$data']";
where: mime can be an image of any kind, text, word document, text document, pdf document, e.t.c... data is blob's column content
其中:mime可以是任何类型的图像,文本,word文档,文本文档,pdf文档,e.t.c ...数据是blob的列内容
#1
91
Try Like this.
试试这样。
For Inserting into DB
用于插入DB
$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$image = addslashes(file_get_contents($_FILES['images']['tmp_name']));
//you keep your column name setting for insertion. I keep image type Blob.
$query = "INSERT INTO products (id,image) VALUES('','$image')";
$qry = mysqli_query($db, $query);
For Accessing image From Blob
用于从Blob访问图像
$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';
Hope It will help you.
希望它会帮助你。
Thanks.
谢谢。
#2
11
This is what I use to display images from blob:
这是我用来显示blob图像的方法:
echo '<img src="data:image/jpeg;base64,'.base64_encode($image->load()) .'" />';
#3
0
Since I have to store varius types of content in my blob field/column, suppose to update my code like this:
因为我必须在我的blob字段/列中存储varius类型的内容,所以假设更新我的代码,如下所示:
echo "data: $mime" $result['$data']";
where: mime can be an image of any kind, text, word document, text document, pdf document, e.t.c... data is blob's column content
其中:mime可以是任何类型的图像,文本,word文档,文本文档,pdf文档,e.t.c ...数据是blob的列内容