【问题标题】:Unable to retrieve image data from table and display image无法从表中检索图像数据并显示图像
【发布时间】:2015-04-26 20:48:33
【问题描述】:

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

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

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

代码:

PHP:

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

【问题讨论】:

    标签: php mysql image mysqli


    【解决方案1】:

    首先,您需要将输出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-&gt;bind_param('sb', $title, $content); 更改为 $statement-&gt;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);
                }
    
            }        
        }
    }
    

    希望它也能正常工作,我已经对其进行了测试并且运行良好...:)

    【讨论】:

    • 你可以使用第一个代码$data = file_get_contents("ACL.jpg"); $img = imagecreatefromstring($data); header("Content-Type: image/jpeg"); imagejpeg($img);测试它,你也可以测试它...... :)
    • 我已经完成了所有这些。没有提供语句错误。弹出的错误是“图像数据不存在!”当表中的图像数据显示为 NULL 时。当我上传它时,很明显二进制数据被放置在表中,但在检索时显示为空
    • @The_Perfect_Username : 我已经编辑了我的答案,我也测试了它,希望它运行良好,如果仍然有错误请告诉我... :)
    猜你喜欢
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 2014-07-20
    相关资源
    最近更新 更多