无法从表格和显示图像中检索图像数据 [英] Unable to retrieve image data from table and display image

查看:129
本文介绍了无法从表格和显示图像中检索图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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.

代码:

PHP:

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

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 并显示你的图像。

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

}



编辑修正:



uploads.php

在此档案中,您需要更改 $ statement-> bind_param('sb',$ title,$ content); 成为 $ statement-> bind_param('ss',$ title,$ content);

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
然后在你用它显示它:

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

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

        }        
    }
}

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

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

这篇关于无法从表格和显示图像中检索图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆