1
夥計們。 我試圖用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「'等)? – ircmaxell 2010-10-02 12:37:09
我認爲'<' for '
'實際上存在於您的代碼中,並且僅僅是您的示例代碼中缺少的? –
Wrikken
2010-10-02 12:37:58
更不用提你在圖像代碼中存在的[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)漏洞...強制'$ _GET ['id']'是一個整數一個字符串。因此而不是'$ tkey =「」。 $鍵。 「」;',做一些事情:'$ tkey =(int)$ key;'。當你連接它時,PHP將自動從int轉換爲一個字符串... – ircmaxell 2010-10-02 12:44:27