无法从 table 检索图像数据并显示图像

Unable to retrieve image data from table and display image

我正在尝试使用 PHP 和 MySQLi 将图像文件上传到我的数据库,但我遇到了一个非常令人困惑的错误。 table 有两列,'title' 和 'image'。 'title' 是文件名,'image' 是图像数据。 table 都不允许接受 NULL。当我上传文件时,数据存储在 table 列中。 'title' 包含正确的值,但 'image' 列包含“<binary data>”。

由于 table 列不接受 NULL 值,我假设它是文件的数据,但是当我尝试在 showimage.php 中检索和显示图像数据时,它告诉我图像数据为空。

我正在使用 BLOB 数据类型将图像数据存储在 table 中。就我而言,基于在线资源和示例,它应该可以工作。谢谢。

代码:

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>

首先,您需要将输出 file_get_contents 中的图像保存到您的数据库中。然后你把它放到 imagecreatefromstring 并显示你的图像。

这是一个简单的例子。也许这对你有帮助:)

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

编辑:

你只需要输入这段代码:

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

}

编辑修复:

uploads.php

在此文件中,您需要将 $statement->bind_param('sb', $title, $content); 更改为 $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 然后你用这个来展示它:

$img = imagecreatefromstring($image); header("Content-Type: image/jpeg"); imagejpeg($img); 在你最后的声明中

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

        }        
    }
}

希望这个也能正常工作,我已经测试过了并且它 运行 很好...:)