mysql 数据库中的图像未在浏览器中显示

发布于 2024-11-05 20:58:51 字数 1728 浏览 0 评论 0原文

我正在尝试显示存储在 MySql 数据库中的图像。

我有下表

CREATE TABLE `Pictures` (
`Id` varchar(36) character set utf8 NOT NULL,
`Name` varchar(100) character set utf8 NOT NULL,
`Type` varchar(25) character set utf8 NOT NULL,
`Size` varchar(25) character set utf8 NOT NULL,
`Picture` mediumblob NOT NULL,
PRIMARY KEY  (`Id`)
) ENGINE=InnoDB;

php 代码具有以下结构

Repostory
- 服务
- - Image.php

存储库

public static function GetPictureById($id) 
{
    global $db;
    $d = $GLOBALS["db"];
    $result = $d->prepare("SELECT * FROM Pictures WHERE id = :id");
    $result->bindParam(":id", $id, PDO::PARAM_STR, 36);

    if($result->execute() !== true)
    {
        Pre($result->errorInfo());
        return;
    }

    $result->bindColumn("Id", $id);
    $result->bindColumn("Type", $type);
    $result->bindColumn("Size", $size);
    $result->bindColumn("Picture", $picture, PDO::PARAM_LOB);
    while($result->fetch(PDO::FETCH_BOUND)) 
    {
        $pic = new Picture();
        $pic->SetId($id);
        $pic->SetType($type);
        $pic->SetSize($size);
        $pic->SetPicture($picture);
    }

    return $pic;
}

服务

public static function GetPictureById($id) 
{
    return PictureRepository::GetPictureById($id);
}

Image.php

error_reporting(E_ALL);
$root = $_SERVER["DOCUMENT_ROOT"]."/new";        
include_once($root . "/Config/Config.php");     
$picture = PictureRepository::GetPictureById($_GET["id"]);  
header("Content-type:". $picture->GetType());
echo $picture->GetPicture();

不幸的是 IE 显示了一个带有红叉的块,而 Firefox 告诉图像包含错误

谁能看到解决方案?

I'm trying to display an image that is stored in a MySql Database.

I have the following table

CREATE TABLE `Pictures` (
`Id` varchar(36) character set utf8 NOT NULL,
`Name` varchar(100) character set utf8 NOT NULL,
`Type` varchar(25) character set utf8 NOT NULL,
`Size` varchar(25) character set utf8 NOT NULL,
`Picture` mediumblob NOT NULL,
PRIMARY KEY  (`Id`)
) ENGINE=InnoDB;

The php code haves the following structure

Repostory
- Service
- - Image.php

Repository

public static function GetPictureById($id) 
{
    global $db;
    $d = $GLOBALS["db"];
    $result = $d->prepare("SELECT * FROM Pictures WHERE id = :id");
    $result->bindParam(":id", $id, PDO::PARAM_STR, 36);

    if($result->execute() !== true)
    {
        Pre($result->errorInfo());
        return;
    }

    $result->bindColumn("Id", $id);
    $result->bindColumn("Type", $type);
    $result->bindColumn("Size", $size);
    $result->bindColumn("Picture", $picture, PDO::PARAM_LOB);
    while($result->fetch(PDO::FETCH_BOUND)) 
    {
        $pic = new Picture();
        $pic->SetId($id);
        $pic->SetType($type);
        $pic->SetSize($size);
        $pic->SetPicture($picture);
    }

    return $pic;
}

Service

public static function GetPictureById($id) 
{
    return PictureRepository::GetPictureById($id);
}

Image.php

error_reporting(E_ALL);
$root = $_SERVER["DOCUMENT_ROOT"]."/new";        
include_once($root . "/Config/Config.php");     
$picture = PictureRepository::GetPictureById($_GET["id"]);  
header("Content-type:". $picture->GetType());
echo $picture->GetPicture();

unfortunately IE shows a block with a red cross and Firefox tells the image contain errors

Who sees the solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗笺 2024-11-12 20:58:51

问题是您对 echo 的使用。

echo 用于打印字符串。您需要将二进制数据写入 STDOUT 流。

为此,您需要使用 fwrite

像这样的东西:

fwrite(STDOUT,$picture->GetPicture());

The problem is your use of echo.

echo is for printing strings. You need to write binary data to the STDOUT stream.

To do this, you need to use fwrite.

Something like:

fwrite(STDOUT,$picture->GetPicture());
忆依然 2024-11-12 20:58:51

下载图像(右键单击/另存为),然后将其加载到文本编辑器中,查看文件开头是否有任何 PHP 警告/错误。您有 E_ALL 用于错误报告,因此脚本中任何地方的单一语法异常都会发出警告并损坏数据流。

Download the image (right click/save as) and then load it up in a text editor, see if there's any PHP warnings/errors at the start of the file. You've got E_ALL for error reporting, so a single syntax oddity anywhere in your scripts will spit out a warning and corrupt the data stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文