我使用PHP和Mysql将图像存储在MySQL数据库中。图像存储在名为B_Image的Blob字段中。但是,当我尝试检索图像时,它会显示字符而不是图像。
我知道我仍然需要编写一些代码来验证某些细节。但首先我想尝试看看它是否会上传和检索图像。
请你帮我看一下图片。
我的UploadImage.php代码:
include"config.php"; //code to connect to my database
$file =$_FILES['txtBookCover']['tmp_name'];
$Title = $_POST[txtBookName];
$Author = $_POST[txtBookAuthor];
$ISBN = $_POST[txtISBN];
$Type = $_POST[lstGenre];
$image = addslashes(file_get_contents ($_FILES['txtBookCover']['tmp_name']));
echo $image_name = addslashes($_FILES['txtBookCover']['name']);
$image_size = getimagesize($_FILES['txtBookCover']['tmp_name']);
$SqlStatement= "INSERT INTO Books(B_Title, B_Author, B_ISBN, B_Image, B_ImageName) VALUES('$Title', '$Author', '$ISBN','$image','$image_name')";
if (mysql_query($SqlStatement)) {
print "Image has been uploaded!";
}
else
{
print "Error uploading image";
}
我的DisplayImg.php代码:
include"config.php";
$SqlStatement="select B_Image,B_Author,B_ID FROM Books where B_Author='ff'";
$result=mysql_query($SqlStatement);
if (mysql_query($SqlStatement)) {
echo"hellooo";
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo
"Author : {$row['B_Author']} <br>" .
"ID : {$row['B_ID']} <br><br>";
echo "<br>";
header('Content-type: image/jpg');
echo "Image: {$row['B_Image']}<br>";
}
}
else
{
echo "error";
}
答案 0 :(得分:1)
我认为将图像存储在数据库中是一个坏主意。通常我们只存储在数据库中的是图像的路径,图像本身存储在服务器上。祝好运。 :)