PHP将来自MYSQL的jpeg回显到HTML页面

时间:2014-01-29 03:31:00

标签: php html mysql codeigniter

我想在HTML页面中输出我用M file_get_contents("http://www.example.com/img.jpg")存储在MYSQL中的图像。我有以下代码,但我不知道如何使这项工作。现在,它显示乱码或如果我使用标题,一个破碎的图像。我正在使用codeigniter:

型号:

public function img_get() {
        $query = $this->db->query('SELECT * FROM imgtable');
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
        }
        return $data;
    }

控制器:

class Site extends CI_Controller {

public function index() {
    $this->load->model('img_model');

    $data['row'] = $this->img_model->img_get();

    $this->load->view('home_view', $data);
}

查看:

<html>
<head>
<title>Homepage</title>
</head>
<body>

<h3>Insert the image url</h3>
<br>

<?php 
    foreach ($row as $r) {
        echo $r->img . "<br />";
    }

?>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

您正在尝试显示BLOB数据:

displaying an image stored in a mysql blob

Show a BLOB image PHP MySQL along with other data

How to display an BLOB image stored in MySql database?

诀窍是使用:

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

归功于本网站上的所有其他问题和答案:)

希望这有帮助!

答案 1 :(得分:1)

你不能这样做,因为http需要一个标题才能知道内容中的内容。

<?php
  foreach($row as $r)
  {
     echo "<img src='yourcontroller?id=your_image_id'></br>";
  }
?>

答案 2 :(得分:1)

您必须先在系统中上传该图像。这样您就可以随时使用您网站的基本网址访问该图片。