I've been trying my head at this for ages, check every possible place on Google, and sometimes I feel brain dead by just doing this.
I'm doing this for the fun of it but I am stuck on this particular aspect of displaying an image from a MySQL database using BLOB.
As far as I know I think I am uploading the image correctly, but whenever I try to display the image, it ends up just giving me the default image icon and the name of the image, Then when I inspect source, it comes up with gibberish for the image.
Can someone please tell me what I'm doing wrong?
show_profile.php
<html>
<body>
<a href="main_page.php">View Records</a>
<a href="logout.php">Logout</a>
<div id="body_show">
<link href="show_profile.css" rel="stylesheet" type="text/css">
<div id="image" class="image" style="width: 152px; height:152px;">
<?php
include('database_connection.php');
if(session_id() ==""){
session_start();
}
$username = $_SESSION['myusername'];
$query = "SELECT * FROM $dImage_table WHERE username='$username' and imageID = (SELECT MAX(imageID) FROM $dImage_table WHERE username='$username')";
$result=mysqli_query($DBConn, $query);
if(!$result){
$message = "<p>
There was an error with the query.<br />\n" .
"The error was " .
htmlspecialchars(mysqli_error($DBConn), ENT_QUOTES) .
".<br />\nThe query was '" .
htmlspecialchars($query, ENT_QUOTES ) .
"'</P>\n";
}
else if (!mysqli_num_rows($result)){
$message = "<p>Cannot find Image in the database.</p>\n";
}
else{
while($report = mysqli_fetch_assoc($result)){
$imageData = $report['image'];
$imageName = $report['imageName'];
$imageType = $report['imageType'];
$imageID = $report['imageID'];
//echo "<img src="'data:image;base64,'.$imageData."' alt='Image'/>";
//echo "<img src=data:$imageType;base64,base64_encode($imageData) alt=$imageName width=152px height=152px/>";
echo '<img src="data:'.$imageType.';base64,'. base64_encode($imageData).'" alt="'.$imageName.'" width="152px" height="152px" />';
//echo "</br>";
}
//echo $imageData;
}
?>
</div>
<div class="form_body">
<form id="form_body" action="pic_uploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image"><input type="submit" name="submit" value="Upload">
</form>
<div>
<?php
//echo $message;
?>
</div>
</body>
pic_uploader.php
<?php
include('database_connection.php');
session_start();
if(isset($_POST['submit']))
{
$username = $_SESSION['myusername'];
$imageName = mysqli_real_escape_string($DBConn, $_FILES["image"]["name"]);
$imageData = mysqli_real_escape_string($DBConn, file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysqli_real_escape_string($DBConn, $_FILES["image"]["type"]);
$imageData= base64_encode($imageData);
if(!substr($imageType,0,4) == "image")
{
$message = "<p>Only Images are allowed!</p>";
include 'show_profile.php';
}
else{
$query = 'INSERT INTO dImage_table (username, imageID, imageName, imageType, image)
VALUES("'.$username.'",
"'. "" .'",
"'.$imageName.'",
"'.$imageType.'",
"'.$imageData.'")';
if(!mysqli_query($DBConn, $query))
{
$message = "<p>
There was an error uploading the image.<br />\n" .
"The error was " .
htmlspecialchars(mysqli_error($DBConn), ENT_QUOTES) .
".<br />\nThe query was '" .
htmlspecialchars($query, ENT_QUOTES ) .
"'</P>\n";
}
else
{
$message = "<p>Image uploaded</p>";
include 'show_profile.php';
}
}
}
?>
Thanks for your input