2

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






?>
user2864740
  • 60,010
  • 15
  • 145
  • 220
dev_marshell08
  • 1,051
  • 3
  • 18
  • 40

1 Answers1

0

I replaced

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

with

ob_start( );

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

dev_marshell08
  • 1,051
  • 3
  • 18
  • 40
  • 2
    The problem is that you where _“lying”_ to the browser with that header – you said that the data he was going to receive was an image, where in fact it is HTML code … (And simply removing the header should have been enough, `ob_start` should not be necessary.) – CBroe May 10 '14 at 23:38
  • @CBroe your are correct i was indeed sending HTML code to the browser it had nothing to do with the ob_start(). This explain it all thanks alot I am new to PHP. – dev_marshell08 May 10 '14 at 23:42