无法显示带有数据URI的BLOB图像 [英] Unable to display BLOB image with Data URI

查看:117
本文介绍了无法显示带有数据URI的BLOB图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySql数据库表中的列中存储了一个jpg图像,其数据类型为BLOB,表明部分php代码可以正常工作.

I have a jpg image stored in MySql Database table in the column with the data type as BLOB that part of the php code works fine.

我正在尝试使用下面的php代码显示该图像,但它将无法正常工作.我在屏幕上看到一个小图标,它绝对不是图像吗?有什么问题吗?

I am trying to display that image using the below php code but it would not work. I see a small icon on the screen which is definitely not the image ? what's wrong any help?

1)读取图像php文件

1) Read the image php file

   <?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数据库

2) Upload the file into the MySql Database

<?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";
}






?>

推荐答案

我已替换

header("Content-Type:image/jpg");

header("Content-Type: image/jpg");

ob_start();

ob_start( );

现在工作正常,我不确定以前是什么问题?

it now works fine i am not sure what was the problem before ?

这篇关于无法显示带有数据URI的BLOB图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆