简体   繁体   中英

store image into mysql DB with php and display

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM