Unable to retrieve image data from table and display image

北城以北 提交于 2019-12-25 02:57:16

问题


I'm trying to upload an image file to my database with PHP and MySQLi, but I'm coming across a very confusing piece of error. The table has two column, 'title' and 'image'. The 'title' is for the file's name and 'image' for the image data. both tables are NOT allowed to accept NULLs. When I upload a file, data is stored into the table columns. 'title' contains the right values, but the 'image' column contains '<binary data>'.

Since the table column does not accept NULL values I assumed that it is the file's data, but when I try to retrieve and display the image data in showimage.php it tells me the image data is NULL.

I am using the BLOB datatype to store the image data in the table. As far as I am concerned based off the online resources and examples it should work. Thanks.

Code:

PHP:

uploads.php

if (isset($_POST['submit'])) {
    $title = $_FILES['image']['name'];
    $data = $_FILES['image']['tmp_name'];
    $content = file_get_contents($data);
    $query = "INSERT INTO images (title, image) VALUES (?, ?)";
    $statement = $databaseConnection->prepare($query);
    $statement->bind_param('sb', $title, $content);
    $statement->execute();
    $statement->store_result();
    $creationWasSuccessful = $statement->affected_rows == 1 ? true : false;
    if ($creationWasSuccessful)
    {
        echo "Works!";
    } else {
        echo 'failed';
    }
}

showimage.php

if (isset($_GET['id'])) {

    $id = $_GET['id'];
    $query = "SELECT * FROM images WHERE id = ?";
    $statement = $databaseConnection->prepare($query);
    $statement->bind_param('i', $id);

    $statement->execute();
    $statement->store_result();

    if ($statement->num_rows >= 1)
    {
        $statement->bind_result($imageid, $title, $image)
        while ($statement->fetch()) {
            if ($image == NULL) {
                echo "Image data does not exist!";
            } else {
                header("Content-Type: image/jpeg");
                echo $image;
            }

        }        
    }
}

HTML

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

回答1:


First you need to save the image from output file_get_contents to your database. And then you put that to imagecreatefromstring and show your image.

Here's the simple example. Maybe this help you out :)

$data = file_get_contents("ACL.jpg");
$img = imagecreatefromstring($data);
header("Content-Type: image/jpeg");
imagejpeg($img);

EDIT :

you just need to put this code :

$statement->bind_result($imageid, $title, $image)
while ($statement->fetch()) {
    if ($image == NULL) {
        echo "Image data does not exist!";
    } else {
        $img = imagecreatefromstring($image);
        header("Content-Type: image/jpeg");
        imagejpeg($img);
    }

}

EDIT FIX :

uploads.php

In this file you need to change your $statement->bind_param('sb', $title, $content); become $statement->bind_param('ss', $title, $content);

if (isset($_POST['submit'])) {
    $title = $_FILES['image']['name'];
    $data = $_FILES['image']['tmp_name'];
    $content = file_get_contents($data);
    $query = "INSERT INTO images (title, image) VALUES (?, ?)";
    $statement = $databaseConnection->prepare($query);
    $statement->bind_param('ss', $title, $content);
    $statement->execute();
    $statement->store_result();
    $creationWasSuccessful = $statement->affected_rows == 1 ? true : false;
    if ($creationWasSuccessful)
    {
        echo "Works!";
    } else {
        echo 'failed';
    }
}

showimage.php and then in you show it using this :

$img = imagecreatefromstring($image); header("Content-Type: image/jpeg"); imagejpeg($img); in your last statement

if (isset($_GET['id'])) {

    $id = $_GET['id'];
    $query = "SELECT id,title,image FROM images WHERE id = ?";
    $statement = $databaseConnection->prepare($query);
    $statement->bind_param('i', $id);

    $statement->execute();
    $statement->store_result();

    if ($statement->num_rows >= 1)
    {
        $statement->bind_result($imageid, $title, $image)
        while ($statement->fetch()) {
            if ($image == NULL) {
                echo "Image data does not exist!";
            } else {
                $img = imagecreatefromstring($image);
                header("Content-Type: image/jpeg");
                imagejpeg($img);
            }

        }        
    }
}

Hope this work fine too, I've tested it and it run well... :)



来源:https://stackoverflow.com/questions/29873722/unable-to-retrieve-image-data-from-table-and-display-image

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