将图像上传到数据库,PHP和Mysql后无法显示
我需要为客户制作一个网页,以将图像上传到数据库并显示它们.
I need to do a web page for a client to upload images to a data base and display them.
我可以将图像上传到数据库中,但我无法显示它们,但我不知道为什么
I am achieve to upload the images into a database, but I'm having trouble displaying them, but I can't work out why
这是我的代码:
<!DOCTYPE html>
<head>
<body>
<form action="form.php" method="post" enctype="multipart/form-data">
File:
<input type="file" name="image" /> <input type="submit" value="Upload" />
</form>
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test" ) or die(mysql_error());
$file = $_FILES['image'] ['tmp_name'];
if (!isset($file)) {
echo "<br>Please select an image.";
}
else {
$image = addslashes(file_get_contents($_FILES['image'] ['tmp_name']));
$imageName = addslashes($_FILES['image']['name']);
$imageSize = getimagesize($_FILES['image']['tmp_name']);
if ($imageSize == FALSE)
echo "<br><br>Thats not an image. <br><br>";
else{
if (!$insert = mysql_query("INSERT INTO imgup VALUES ('','$imageName','$image')"))
echo "Problem uploading the image.";
else{
$lastId = mysql_insert_id();
echo "Article uploaded.<p /> Your image:<p /> <img src=get.php?id=$lastId>";
}
}
}
?>
</body>
</html>
这是我将图像 blob 转换为图像的文件:
This is my file who turn the image blob into an image:
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test" ) or die(mysql_error());
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM blog WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
最后图像不显示,这就是我得到的:http://goo.gl/gi1Uuc
And at the end the image does not display and this is what i get: http://goo.gl/gi1Uuc
如果我去检查我的数据库,图像已经成功上传......
And if i go and check my database, the image has ben successfully uploaded...
推荐答案
根据文件使用内联base64编码.这是通过以下方式完成的:
Depending on the file use inline base64 encoding. This is done with:
echo '<img src="data:image/jpeg;base64,'.base64_encode( $image ).'"/>';
字体:base64_encode
或
把T
大写(Type),因为在IE中会报错.尝试使用函数 file_get_contents 进行打印.
Put the T
upperCase (Type), because can giving error in IE. Try printing with the function file_get_contents.
header('Content-Type: image/jpeg');
echo readfile($image);
相关文章