【发布时间】:2012-09-17 17:15:08
【问题描述】:
伙计们。 我尝试使用 php 加载存储在 mysql blob 字段中的图像,但图像显示不正确。在萤火虫中,我得到了这些信息:get-image.php Dimensions0 × 0File size5.35KBMIME typeimage/jpeg
这是我的代码
HTML
<html>
<head>
<title>Demo of Database Image in a page</title>
</head>
<body>
Here is your picture:<br>
img src=get-image.php?id=1 width=400 height=300><br>
</body>
</html>
PHP
<?php
include "db.php";
$conn = OpenDbConnection();
$key = $_GET["id"];
$tkey = "" . $key . "";
$strsql = "SELECT * FROM `images` WHERE `image_id` = " . $tkey;
$rs = mysql_query($strsql, $conn) or die(mysql_error());
if (!($row = mysql_fetch_array($rs))) {
die("File not exists.");
}
header("Content-type: image/jpeg");
echo $row["content"];
mysql_free_result($rs);
mysql_close($conn);
?>
请有人告诉我我的代码有什么问题?
【问题讨论】:
-
img 标签上是否有开头
<?标签属性上的引号(src="get-image.php?id=1"、width="400"等)呢? -
我假设
<的<img>实际上在您的代码中,只是在您的示例代码中丢失了? -
更不用说图像代码中的SQL Injection 漏洞了...强制
$_GET['id']为整数而不是字符串。因此,不要使用$tkey = "" . $key . "";,而是执行以下操作:$tkey = (int) $key;。当你连接它时,PHP 会自动从 int 转换为字符串... -
您应该下载图像并在文本编辑器中打开它或直接从浏览器调用它。我猜你会得到有用的信息,例如错误消息。
标签: php