Can't display image after uploading it to a data base, PHP and Mysql

試著忘記壹切 提交于 2019-11-28 12:55:51

问题


I need to do a web page for a client to upload images to a data base and display them.

I am achieve to upload the images into a database, but I'm having trouble displaying them, but I can't work out why

Here is my code:

<!DOCTYPE html>
    <head>
    <body>

    <form action="form.php" method="post" enctype="multipart/form-data">
        File:
        <input type="file" name="image" /> <input type="submit" value="Upload" />
    </form>

    <?php 
        mysql_connect("localhost", "root", "") or die(mysql_error());
        mysql_select_db("test" ) or die(mysql_error());

        $file = $_FILES['image'] ['tmp_name'];

        if (!isset($file)) {
            echo "<br>Please select an image.";
        }
        else {
            $image = addslashes(file_get_contents($_FILES['image'] ['tmp_name']));
            $imageName = addslashes($_FILES['image']['name']);
            $imageSize = getimagesize($_FILES['image']['tmp_name']);

            if ($imageSize == FALSE)
                echo "<br><br>Thats not an image. <br><br>";


            else{
                if (!$insert = mysql_query("INSERT INTO imgup VALUES ('','$imageName','$image')"))
                    echo "Problem uploading the image.";
                else{
                    $lastId = mysql_insert_id();
                    echo "Article uploaded.<p /> Your image:<p /> <img src=get.php?id=$lastId>";
                }
            }
        }

        ?>

    </body>
</html>

This is my file who turn the image blob into an image:

<?php 
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("test" ) or die(mysql_error());

        $id = addslashes($_REQUEST['id']);


        $image = mysql_query("SELECT * FROM blog WHERE id=$id");
        $image = mysql_fetch_assoc($image);
        $image = $image['image'];

        header("Content-type: image/jpeg");

        echo $image;

?>

And at the end the image does not display and this is what i get: http://goo.gl/gi1Uuc

And if i go and check my database, the image has ben successfully uploaded...


回答1:


Depending on the file use inline base64 encoding. This is done with:

echo '<img src="data:image/jpeg;base64,'.base64_encode( $image ).'"/>';

Font: base64_encode

OR

Put the T upperCase (Type), because can giving error in IE. Try printing with the function file_get_contents.

header('Content-Type: image/jpeg');
echo readfile($image);



回答2:


I wouldn't store any image in a database. You should save it as file, and store the file's name in the database. You can then configure which directory an image gets served from without worrying about the full path to the image, or storing binary data in your db (yuck).




回答3:


Try changing:

$image = mysql_query("SELECT * FROM blog WHERE id=$id");

to:

$image = mysql_query("SELECT * FROM blog WHERE id = '$id'");



回答4:


Escaping an image file with addslashes will probably corrupt it, the imagesize test should be sufficient



来源:https://stackoverflow.com/questions/23419560/cant-display-image-after-uploading-it-to-a-data-base-php-and-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!