php 如何显示存储在 MySql 数据库中的 BLOB 图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13214602/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:00:05  来源:igfitidea点击:

How to display an BLOB image stored in MySql database?

phphtmlmysqlimageblob

提问by Ktmock13

I am trying to display the last 5 images uploaded to my "store" table in MySql. I'm a complete noob to PHP and databases and i've been reading a lot on how to do this but no luck.

我试图在 MySql 中显示上传到我的“商店”表的最后 5 张图像。我是 PHP 和数据库的完全菜鸟,我已经阅读了很多关于如何做到这一点但没有运气的文章。

I can store and display pictures one at a time but i'd like to be able to have a gallery of sorts to show the last 5 uploaded.

我可以一次存储和显示一张图片,但我希望能够有一个画廊来显示最近上传的 5 张图片。

any advice or help would be greatly appreciated thanks!

任何建议或帮助将不胜感激谢谢!

p.s. I know it frowned upon to store pictures to a database like this but this project is just for practice.

ps 我知道像这样将图片存储到数据库是不受欢迎的,但这个项目只是为了练习。

index.php

索引.php

<!DOCTYPE html>
<html>
<head>
<title>Project One</title>
</head>

<body>

<form action="index.php" method="POST" enctype="multipart/form-data">
    File:
    <input type="file" name="image"> <input type="submit" value="Upload">
<form>
<p />

<?php

//connect to database
(connect to server)
(select correct DB)

//file properties
$file = $_FILES['image']['tmp_name'];

if (!isset($file))
    echo "please select an image.";
else
  {
  $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
  $image_name = $_FILES['image']['name'];
  $image_size = getimagesize($_FILES['image']['tmp_name']); 

  if($image_size==FALSE)
    echo "That's not an image.";
  else
  {
    if (!$insert = mysql_query("INSERT INTO store VALUES ('', '$image_name', '$image')"))
        echo "Problem Uploading Image.";
    else
        {

        $lastid = mysql_insert_id();
        echo "Image uploaded. <p />Your image:<p /><img src=get.php?id=$lastid>";

        }
  }
  }

?>

<p />
<p />
<a href="http://WEBSITE.com/gallery.php"> Go to Gallery </a>
</body>

</html>

get.php

获取.php

<?php

   //connect to database
    (connect to server)
    (select correct DB)

$id = addslashes($_REQUEST['id']);

$image = mysql_query("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-type: image/jpeg");

echo $image;

?>

回答by Manatax

This is what I used when I wanted to do something like that... a long time ago! =P

这就是我想要做那样的事情时使用的……很久以前!=P

$sql = "SELECT image FROM table WHERE cond ORDER BY xxxx DESC LIMIT 5";
$result = mysqli_query($db,$sql);
while($arraySomething = mysqli_fetch_array($result))
{
    echo "<img src='php/imgView.php?imgId=".$arraySomething."' />";
}

回答by jerza

I try the first approach with header('content-type: image/jpeg');but end up with image not shown. After a few google through website I found the solution which I can display image from database to my page

我尝试了第一种方法,header('content-type: image/jpeg');但最终没有显示图像。在通过网站谷歌几次之后,我找到了可以将数据库中的图像显示到我的页面的解决方案

try this:

尝试这个:

mysql_connect("localhost","root","")or die("Cannot connect to database"); //keep your db name
mysql_select_db("example_db") or die("Cannot select database");
$sql = "SELECT * FROM `article` where `id` = 56"; // manipulate id ok 
$sth = mysql_query($sql);
$result=mysql_fetch_array($sth);
// this is code to display 
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image_file'] ).'"/>'

回答by Indeewara Jayalath

mysql_connect("localhost","root","")or die("Cannot connect to database"); 

//keep your db name
mysql_select_db("example_db") or die("Cannot select database");

$sql = "SELECT * FROM `article` where `id` = 56"; 
// manipulate id ok 
$sth = mysql_query($sql);
$result=mysql_fetch_array($sth);
// this is code to display 

echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image_file'] ).'"/> width="xxxx" height="xxxx"';

Add the height and width also

还添加高度和宽度

回答by az fav

You can also use this function

你也可以使用这个功能

//Retrieve image from database and display it on html webpage    
function displayImageFromDatabase(){    
//use global keyword to declare conn inside a function    
global $conn;    
$sqlselectimageFromDb = "SELECT * FROM `imageuploadphpmysqlblob` ";    
$dataFromDb = mysqli_query($conn, $sqlselectimageFromDb);    
while ($row = mysqli_fetch_assoc($dataFromDb)) {    
echo '<img height="250px" width="250px" src=data:image;base64,'.$row['image'].'/>';    
}

Insert it into mysql database like this :

像这样将其插入到 mysql 数据库中:

$image = $_FILES['imagefile']['tmp_name'];
$name = $_FILES['imagefile']['name'];
$image = base64_encode(file_get_contents(addslashes($image)));

references : https://mauricemutetingundi.blogspot.com/2019/04/how-to-upload-blob-image-to-mysql.html

参考资料:https: //mauricemutetingundi.blogspot.com/2019/04/how-to-upload-blob-image-to-mysql.html