PHP 显示来自 MySQL 的图像 BLOB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20556773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP display image BLOB from MySQL
提问by user2732663
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?ebr?
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz??…???‰?'“”?–—???¢£¤¥|§¨?a23 ′μ?·?1o??????èéêòó???×?ùúáa?????èéê?òó???÷?ùú??????????'... (length=60766)
回答by Rizwan Shamsher Kaim Khani
Try Like this.
像这样尝试。
For Inserting into 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.
谢谢。
回答by Arian Faurtosh
This is what I use to display images from blob:
这是我用来显示 blob 图像的方法:
echo '<img src="data:image/jpeg;base64,'.base64_encode($image->load()) .'" />';
回答by Nicholas
Since I have to store various types of content in my blob field/column, I am suppose to update my code like this:
由于我必须在我的 blob 字段/列中存储各种类型的内容,我想像这样更新我的代码:
echo "data: $mime" $result['$data']";
where:
mimecan be an image of any kind, text, word document, text document, PDF document, etc... content datatype is blobin database.
其中:
mime可以是任何类型的图像、文本、word 文档、文本文档、PDF 文档等...内容数据类型blob在数据库中。

