无法从表格和显示图像中检索图像数据

时间:2015-04-26 04:23:49

标签: php mysql image mysqli

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

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

我使用BLOB数据类型将图像数据存储在表中。据我所知,基于在线资源和示例,它应该有效。感谢。

代码:

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 个答案:

答案 0 :(得分:0)

首先,您需要将输出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);
            }

        }        
    }
}

希望这项工作也很好,我已经测试了它并且运行良好......:)