douyi3676
douyi3676
2013-03-16 17:34

使用php和显示将图像存储到mysql DB中

已采纳

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条回答

  • duansao20000508 duansao20000508 8年前

    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.

    点赞 评论 复制链接分享
  • dtbonklcs575884485 dtbonklcs575884485 8年前

    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.

    点赞 评论 复制链接分享

相关推荐