0

Having big time problems in displaying an image out of my mysql database

I'm storing it in a longblob type

when the image is displayed i get a broken image icon

here is code for storing image

if(isset($_FILES['image']) && $_FILES['image']['size'] > 0 && isset($_POST['photoName']))
{
    //temporary file name
    // Temporary file name stored on the server
    $tmpName = $_FILES['image']['tmp_name'];
    $imageType = $_FILES['image']['type'];

    // Read the file
    $fp = fopen($tmpName, 'r');
    $data = fread($fp, filesize($tmpName));
    $data = addslashes($data);
    fclose($fp);


    $sql="INSERT INTO photos (photoName, caption, photoData, photoType, userName)
        VALUES
        ('$_POST[photoName]','$_POST[caption]','$tmpName','$imageType', '$currentUser')";

    //For debugging purposes
    if(!mysqli_query($con,$sql))
    {
        die('Error: ' . mysqli_error($con));
    }
    else
    {
        echo "Your Image has been Added";
    }   
}

then printing the image

if(isset($_POST['usersImage'])){
        //code to show images
        $user = $_POST['usersImage'];
        $sql = "SELECT * FROM `photos` WHERE userName = '$user'";
        $result = mysqli_query($con,$sql);
        while($row = mysqli_fetch_array($result)) 
        {
            switch ($row['photoType']) {
                case 'image/jpeg':

                echo "<tr>";  
                echo '<img src="data:image/jpeg;base64,'. base64_encode($row['photoData'])."\"></td>";   
                echo "</tr>"; 
                    //echo '<img src="image>' .$row['photoData']. '.jpeg'.'</p>';
                    //echo '<p id="caption">'.$row['caption'].' </p>';
                break;
            }           
        }
    }

as you can see my latest attempt was to use base64 encoding to print out the image my previous try was the commented out code

2 Answers 2

3

When you display the image it has to be from its own request. src="" should contain a url to a script that will deliver the content with the correct MIME header image/jpeg.

<?php
    $photo_bin = //binary data from query here;

    header("Content-Type: image/jpeg"); // or whatever the correct content type is.
    echo $photo_bin;

Quick example^ of a php script that can be requested by the browser from an img tag.

Sign up to request clarification or add additional context in comments.

3 Comments

well try going to the url in your browser. if that doesn't work then you're doing something wrong and you will for sure get a broken img icon.
like M.K Price's answer, you have to use addslashes to insert the binary data into the database.
If your database is returning a resource (check with var_dump()) - use stream_get_contents($result); to output the data.
1

Validation is important. You are opening yourself up to so many security issues.

Never use $_POST / $_GET inside a sql statement. Escape them, better yet, use PDO statements. Validate that the image is actually an image, at this point, you could be entering any type of file into your table.

As you're finding, it's also far more difficult to store images in a database than on the filesystem. Usually, there are far more arguments to store the image on the filesystem than inside a table.

Having a quick look down your insertion code, I'm not quite sure why you're adding slashes to the binary data. remove the call to addslahes, and try that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.