I want to display my blob into my PHP but it wont display


if (count($_FILES) > 0) {
    if (is_uploaded_file($_FILES['userImage']['tmp_name'])) {

        $imgData = addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
        $imageProperties = getimageSize($_FILES['userImage']['tmp_name']);

        $sql = "INSERT INTO trial (imageType ,imageData, user_id)
				VALUES('{$imageProperties['mime']}', '{$imgData}','".$_SESSION['id']."')";
        $current_id =  mysqli_query($db, $sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error($db));
		$current_id = mysqli_insert_id($db);
        if (isset($current_id)) {
           header("Location: preview.php");

	<form name="frmImage" enctype="multipart/form-data" action=""
        method="post" class="frmImageUpload">
        <label>Upload QrCode File:</label><br /> 
        <input name="userImage" type="file" class="inputFile" /> 
        <input type="submit" value="Submit" class="btnSubmit" />
require_once '../php_action/database.php';
if(isset($_GET['image_id']) && ctype_digit($_GET['image_id'])) {
	$sql = "SELECT imageType,imageData FROM trial WHERE `user_id` = " . $_SESSION['id'] . " AND  id=" . $_GET['image_id'] . mysqli_error(($db));
	$result = mysqli_query($db, $sql) or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysqli_error($db));
	$row = mysqli_fetch_array($result);
		header("Content-type: ".$row['imageType']);
		echo $row['imageData'];	
		header('Location: preview.php');


    $sql = "SELECT imageID FROM trial ORDER BY imageID DESC LIMIT 1"; 
    $result = mysqli_query($db, $sql);

	while($row = mysqli_fetch_array($result)) {
		<img height="250px" width="250px" src="preview.php?image_id=<?php echo $row["imageId"]; ?>" /><br/>

Please add these two instructions at the top of the relevant PHP files ad let us know if there are any errors:

error_reporting(-1) ;
ini_set('display_errors', 'TRUE');

no error to display

Learn how to debug scripts by adding breakpoints and also to find the variable types, etc"

// useful to find which if/endif, while, branch selected 
  echo '<br> Line ==> ' .__line__ .'<br>' ; 

// show any type of variable and add lines
  echo '<pre>'; print_r($varableName); echo '</pre>';

// Stop execution

FIRST: Are imageID and imageId the same?
SECOND: I can’t say for sure but I doubt loading a webpage path into a scr line is going return an image.
THIRD: On preview.php, NOTHING can be sent to the browser such as a blank line, i.e.


or the header will fail. I have never used Blob to hold images for web display and even though I did get preview.php to display the uploaded image, nothing is sent to the browser before the header. You have <img being sent to the browser but what is loaded to src is not a path to an image.

OK after some WEB searching you can simply display the image like so.

    $sql = "SELECT imageType,imageData FROM trial ORDER BY imageID DESC LIMIT 1"; 
    $result = mysqli_query($db, $sql);
	while($row = mysqli_fetch_array($result)){		
		echo '<img height="250px" width="250px" src="data:'.$row['imageType'].';base64,'.base64_encode($row['imageData']).'"/>';		

Guess I learned something today.:wink:

The width and height parameters should only be numeric and not have the trailing px.


True John. I just copied what he had.
I never even use height and width attributes in image tags anymore.

1 Like

how about if I have two IDs and I want to get the other ID and use it for my other IDs

lets say I have two users :employee and user

All those queries should be user defined with their login session identifier.

FROM trial WHERE `user_id` = " . $_SESSION['id'] . " AND etc...

I endeavour to always specify image dimensions because it allocates the correct space and prevents the page from jumping to insert the image after it is loaded.

1 Like

I usually do not show full sized images but upload three image sizes and in most cases I am calling thumb or tile sized images which load pretty fast.

Most pages I create are usually dynamic in width and most image containers would be a part of that width usually based off the screen width. I usually define the image as the child of the image with width:100% and height:auto; A basic example:

<style type="text/css">
	width: 20%;
.container img{

This container div would go around the image.

echo '<div class="container"><img src="data:'.$row['imageType'].';base64,'.base64_encode($row['imageData']).'"/></div>';

Again very basic example.
Absolutely there are times for a fixed size. It really depends on the application.

I use the same CSS to allow the image to fill the parent container and also specify the image dimensions which I think prevents the page from jumping.

Well true that defining the dimension will load the <img at the specified size before the image is rendered, thus no jumping. So you are absolutely correct.

1 Like

A major annoyance of mine is on a forum site, where I scroll to the end of a thread to see the latest entries and find that I have to keep scrolling down, over and over again, because whoever wrote the forum software doesn’t specify the image size. It’s surprising how many forum software packages don’t do this. I think they all assume we have unlimited bandwidth and speed. Goes back to the thought that developers should have slow hardware and internet access so they can test real-world response times.


Try this:

$dims = getimagesize($fileName)[3]; // width=‘123’ height=‘456’
echo $tmp = <<< ___EOT
   <img src =‘$fileName’ $dims alt=‘$fileName’>

no image to show if put this WHERE user_id = " . $_SESSION[‘id’] . "

What is in $_SESSION['id']? Is there a valid user of that ID, and does it have an image to display?

In the opening comment you are saving the image with $_SESSION[‘id’] being the value for user_id. So it is assumed you are dealing with login system where there is a $_SESSION[‘id’] for each user. In your trial table, what values are saved thus far during testing? Is it the ID for the user?