【问题标题】:Display all images stored as BLOB data from MySQL database显示存储为 MySQL 数据库中 BLOB 数据的所有图像
【发布时间】:2014-08-05 01:48:34
【问题描述】:

我已经使用 PHP 将一些图像上传到 MySQL。每次修改 HTML img 标签中的 id 时,我也可以显示 1 张图像。 但现在我正在尝试显示存储在 MySql 数据库中的所有图像,问题是当我使用“While Loop”时,它只显示文本列,而不显示存储为 MySQL 中 BLOB 数据的图像......

我有一个名为:my_db 的数据库 我有一个名为:blob 的表 我的表中有 3 列:idname & image

这里是 index.php 的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

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

if(isset($_POST['submit']))
{
    mysql_connect("127.0.0.1","root","");
    mysql_select_db("my_db");

    $imageName = mysql_real_escape_string($_FILES["image"]["name"]);
    $imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
    $imageType = mysql_real_escape_string($_FILES["image"]["type"]);

    if(substr($imageType,0,5) == "image")
    {
        mysql_query("INSERT INTO `blob` VALUES('','$imageName','$imageData')");
        echo "Image Uploaded!";
    }
    else
    {
        echo "Only images are allowed!";
    }

}

?>

<img src="showimage.php?id=11">

</body>
</html>

这里是 showimage.php 的代码:

<?php

mysql_connect("127.0.0.1","root","");
mysql_select_db("my_db");

if(isset($_GET['id']))
{
    $id = mysql_real_escape_string($_GET['id']);
    $query = mysql_query("SELECT * FROM `blob` WHERE `id`='$id'");
    while($row = mysql_fetch_assoc($query))
    {
        $imageData = $row["image"];
    }
    header("content-type: image/jpeg");
    echo $imageData;
}
else
{
    echo "Error!";
}

?>

提前致谢:-)

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    在控制器中:

    function display()
    {
        $imagename= mysql_real_escape_string($_FILES["userfile"]["name"]);
    
        $imagedata= mysql_real_escape_string(file_get_contents($_FILES["userfile"]["tmp_name"]));
        $imagetype= mysql_real_escape_string($_FILES["userfile"]["type"]);
    
        $imagesize= mysql_real_escape_string($_FILES["userfile"]["size"]);
        $imagedetails = array(  'image_name'    => $imagename,
                                    'image_data'   => $imagedata
                );
       // $insertedid =$this->model_data->insert_image($imagedetails);
        $this->load->model('model_data');   
        $data['imagee'] = $this->model_data->display_pic($insertedid);  // variable data holds image                
        $this->load->view('profile_view',$data);            
    }   
    

    在模型中:

    function display_pic($insertedid)
    {   
        $query = mysql_query("SELECT * FROM employee_image WHERE id = ".$insertedid);
        $row = mysql_fetch_assoc($query);
        $this->db->stop_cache();
        return $row['image_data'];
    }
    
    function insert_image($imagedetails) { 
    
        $this->db->insert($this->table_name2,$imagedetails);            
        $id=$this->db->insert_id(); 
        return $id;
    }
    

    【讨论】:

    • 将视图代码粘贴到评论中的目的是什么?它绝对不可读。重新审视您的问题,单击编辑,并将其放在它所属的位置。然后,删除评论。此外,添加一些文字来解释您的答案。
    【解决方案2】:

    首先您需要删除if(isset($_GET['id']) 语句,因为您要显示所有图像。

    接下来,通过将查询更改为不带 id 的查询来查询所有图像

    $query = mysql_query("select * from `blob`");
    

    接下来,将图像数据存储到数组中。

    $images = array();
    while ($row = mysql_fetch_assoc($query)) {
      $images[] = $row['image'];
    }
    

    最后,显示所有图像。 (得到代码以显示来自here 的多个 blob)

    /* header should be removed
       header("content-type: image/jpeg"); */
    foreach ($images as $image) {
      echo '<img src="data:image/jpeg;base64,'. base64_encode($image) .'" />';
    }
    

    【讨论】:

    • 当我尝试该代码时,我得到一个错误“图像无法显示,因为它包含错误”??
    • @user3740970,不用header("content-type: image/jpeg")可以试试吗?
    • 然后我得到所有图像的损坏的图像图标(比如在 HTML 中写入错误的路径),没有错误...
    • @user3740970,我犯了在base64_encode($image) 后面加上; 的错误。它已从答案中删除,但我认为这不是问题所在。你能用你当前的代码更新你的帖子吗?
    • 但是有一个问题,当我更新页面并显示图像时,它们会在 1 分钟或类似的情况下再次消失??
    猜你喜欢
    • 2012-10-24
    • 2014-01-05
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 2016-02-09
    相关资源
    最近更新 更多