Question

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...

Was it helpful?

Solution

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);

OTHER TIPS

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).

Try changing:

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

to:

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top