mysql 数据库中的图像未在浏览器中显示
我正在尝试显示存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您对
echo
的使用。echo
用于打印字符串。您需要将二进制数据写入 STDOUT 流。为此,您需要使用
fwrite
。像这样的东西:
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:
下载图像(右键单击/另存为),然后将其加载到文本编辑器中,查看文件开头是否有任何 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.