0

I was trying to figure out how to upload an image into mysql then display it. I got the uploading part set. The problem I am have is displaying it. When I go to display it does one of the following:

  1. Displays no image but only has a little image icon 10x10px image on top left
  2. Displays the whole string of the image and then properly renders the image.

When I remove/comment out the echo $contentIMG statement, nothing displays.

<?php

include 'connections/conn.php';

 error_reporting(E_ALL);
 // some basic sanity checks
 if(isset($_GET['id']) && is_numeric($_GET['id'])) {

     $contentIMG = $row_Recordset1['content'];
    echo $row_Recordset1['content'];
     //echo $contentIMG;

     // set the header for the image
    //header("Content-type: image/jpeg");
    header("Content-type: image/jpeg\n\n"); 
     echo "where is the image #1<br>";
    //echo '<img src="data:image/jpeg;base64,'.$contentIMG.'"/>';
    echo "where is the image #2<br>";
    echo '<img src="data:image/jpeg;base64,'.base64_encode( $contentIMG ).'"/>';
     if (isset($contentIMG)){
            //echo '<img src="data:image/jpg;base64,'.base64_encode($contentIMG).'"  />';
        }
     //echo mysql_result($result, 0);

     // close the db link
     mysql_close($result);
 }
 else {
     echo 'Please use a real id number';
 }


 echo "break <br>";
 $img=base64_encode($row_Recordset1['content']);

?>

I looks like this enter image description here

miken32
  • 42,008
  • 16
  • 111
  • 154
moore1979
  • 3
  • 2
  • You should read this: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – miken32 Dec 05 '15 at 00:10

1 Answers1

0

I may be wrong but I think that when you set your header Content-type to image/jpeg, you should just return the image data (assuming it is stored as a blob in your database)

<?php 
    header('Content-type: image/jpeg');
    echo $contentIMG; 
?>

Your code should look like this:

<?php

include 'connections/conn.php';

 error_reporting(E_ALL);
 // some basic sanity checks
 if(isset($_GET['id']) && is_numeric($_GET['id'])) {

     $contentIMG = $row_Recordset1['content'];
     header('Content-type: image/jpeg');
     echo $row_Recordset1['content'];

     // close the db link
     mysql_close($result);
 }
 else {
     echo 'Please use a real id number';
 }

?>