PHP/MySQL-显示Blob中的图像 [英] PHP/MySQL - show image from blob

查看:138
本文介绍了PHP/MySQL-显示Blob中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个php页面,您可以在其中上传图像/文件,这些图像/文件将以"mediumblob"类型保存在数据库中.现在,我陷入了困境,无法弄清楚如何从数据库中显示这些图像.我用大写字母写了一些要打印的图像.

I've made a php-page where you can upload images/files that will be saved in my database as the type "mediumblob". Now I'm stuck and can't figure out how to display these images from the database. I wrote in caps where I would like to print out the images.

<?php
$dbLink = new mysqli('127.0.0.1', 'root', '', 'test');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}

$sql = 'SELECT `id`, `name`, `mime`, `size`, `created`, `data` FROM `file`';
$result = $dbLink->query($sql);

if($result) {
// Kolla om det finns några filer i databasen
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {

echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>Mime/type</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b>Image</b></td>
</tr>';

while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['mime']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
//THIS IS WHERE I'M SUPPOSED TO PRINT OUT THE IMAGES
</tr>";
}



echo '</table>';
}
}

else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
echo '<p>Click <a href="index.html">here</a> to go back</p>';
// Close the mysql connection
$dbLink->close();
?>

这是我在数据库中的表:

This is my table in the database:

CREATE TABLE `file` (
`id` Int Unsigned Not Null Auto_Increment,
`name` VarChar(255) Not Null Default 'Untitled.txt',
`mime` VarChar(50) Not Null Default 'text/plain',
`size` BigInt Unsigned Not Null Default 0,
`data` MediumBlob Not Null,
`created` DateTime Not Null,
PRIMARY KEY (`id`)
) 

非常感谢您的帮助!

推荐答案

有两种方法可以做到这一点:

There is two approach for doing this:

您可以使用内联base64编码.如果您的图片是jpeg,则可以通过这种方式进行:

You could use inline base64 encoding. You could do it this way assuming your images are jpeg:

echo '<td><img src="data:image/jpeg;base64,'.base64_encode($image);.' /></td>';

下一个选项是创建一个名为image.php的PHP文件,该文件将图像的ID作为参数,然后输出图像.您的HTML如下所示:

The next option would be to create a PHP file called image.php that takes the ID of the image as parameter and then output the image. Your HTML would then look like this:

<td><img src="image.php?id={$row['id']}"></td>

无论如何,关于这个问题有很多被回答的话题.例如,您可以看一下这个: 我需要我的PHP页面显示我的mysql数据库中的BLOB图像

Anyway, there is numerous of answered topics on this matter. You may have a look at this one for example: I need my PHP page to show my BLOB image from mysql database

这篇关于PHP/MySQL-显示Blob中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆