-1

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

Kebab Programmer
  • 1,213
  • 2
  • 21
  • 36
  • You're most likely outputting before header for the show_profile.php file with the HTML over top of `session_start();`. Use error reporting http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Nov 29 '15 at 22:10

1 Answers1

0

Upload the image to a web server and store the url of the image, never seen anyone store the image directly into a mysql database. Here's a simple tutorial on how to upload files: http://www.w3schools.com/php/php_file_upload.asp

patrickdamery
  • 539
  • 6
  • 16
  • *"ever seen anyone store the image directly into a mysql database"* - It's been done quite often, it's called a BLOG or LONGBLOB http://dev.mysql.com/doc/refman/5.7/en/blob.html - Not always recommended but still valid/allowed. – Funk Forty Niner Nov 29 '15 at 22:07
  • Plus, your answer isn't what the question is about. The OP is having problems displaying from BLOB. OP wrote *"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."* and *"As far as I know I think I am uploading the image correctly, but whenever I try to display the image"* – Funk Forty Niner Nov 29 '15 at 22:08
  • @Fred-ii- OP wants to store an image and display it, that is what the question is about. My solution is an improvement of what he is trying to do: http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – patrickdamery Nov 30 '15 at 22:37
  • @Fred-ii- And what I say still stands, don't know anyone who stores images directly into a database instead of using the method I described above, maybe you've had situations where you found that storing image in db was a better method, if you have I'd be curious to know why? – patrickdamery Nov 30 '15 at 22:43
  • Everything has its pros & cons. – Funk Forty Niner Nov 30 '15 at 22:58
  • @Fred-ii- that's a very vague answer, care to explain to me what the pros are? And when it would be better to store images in the database? – patrickdamery Dec 01 '15 at 11:46