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

时间:2012-11-03 23:38:30

标签: php html mysql image blob

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

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

非常感谢任何建议或帮助,谢谢!

P.S。我知道将图片存储到像这样的数据库是不礼貌的,但这个项目只是为了练习。

的index.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

   //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;

?>

4 个答案:

答案 0 :(得分:6)

这就是我想做类似事情的时候......很久以前! = 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."' />";
}

答案 1 :(得分:6)

我尝试使用header('content-type: image/jpeg');的第一种方法,但最终图像未显示。经过一些谷歌浏览网站后,我找到了一个解决方案,我可以将数据从数据库显示到我的页面

试试这个:

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'] ).'"/>'

答案 2 :(得分:2)

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"';

同时添加高度和宽度

答案 3 :(得分:1)

您也可以使用此功能

//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'].'/>';    
}

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

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

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