无法使用数据URI显示BLOB图像

时间:2014-05-10 05:11:59

标签: php mysql data-uri

我有一个jpg图像存储在MySql Database表的列中,数据类型为BLOB,部分php代码正常工作。

我试图使用下面的PHP代码显示该图像,但它不起作用。我在屏幕上看到一个小图标,绝对不是图像?什么帮忙有什么不对?

1)阅读图像php文件

   <?php
    header("Content-Type: image/jpg");
    $db=mysqli_connect("localhost","root","root123","deal_bank","3306");

    if (mysqli_connect_errno())
      {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    mysqli_select_db($db,"deal_bank");

    $sql = "SELECT * FROM image";
    $sth = $db->query($sql);
    $result=mysqli_fetch_array($sth);
    echo '<img src="data:image/jpg;base64,'.base64_encode( $result['image'] ).'"/>';


    ?>

2)将文件上传到MySql数据库

<?php

$con=mysqli_connect("localhost","root","root123","deal_bank","3306");

if (mysqli_connect_errno())
  {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_select_db($con,"deal_bank");

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] > 20000)
&& in_array($extension, $allowedExts)) {
  if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
  } else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    if (file_exists("upload/" . $_FILES["file"]["name"])) {
      echo $_FILES["file"]["name"] . " already exists. ";
    } else {

    $stmt = $con->prepare('INSERT INTO image (image) VALUES (?)');

$null = null;
$stmt->bind_param('b', $null);
$stmt->send_long_data(0, file_get_contents($_FILES['file']['tmp_name']));

$stmt->execute();

      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

     // $image = addslashes(file_get_contents($_FILE['file']['tmp_name']));
      //mysqli_query($con,"INSERT INTO image (image) VALUES ('{$image}') ");


    }
  }
} else {
  echo "Invalid file";
}






?>

1 个答案:

答案 0 :(得分:0)

我替换了

  

标题(“Content-Type:image / jpg”);

  

ob_start();

它现在工作正常我不知道之前是什么问题?