Unable to display BLOB image with Data URI

非 Y 不嫁゛ 提交于 2019-12-11 00:54:44

问题


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.

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






?>

回答1:


I replaced

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

with

ob_start( );

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



来源:https://stackoverflow.com/questions/23577328/unable-to-display-blob-image-with-data-uri

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!