无法显示通过PHP上传的图像到MySQL
问题描述:
我已经修剪我的代码到最低限度,试图找出为什么我不能显示任何图像,我通过PHP上传到MySQL的任何图像。如果有人能指出我的错误,我会非常感激。无法显示通过PHP上传的图像到MySQL
执行时,浏览器报告图像不能显示,因为它包含错误。
但是,图像上传&在同一环境中运行的其他数据库中显示正常。
我已检查数据库上传后是否保留有斑点。
我想我错过了一些明显的东西。
上传的形式..
<body>
<form enctype="multipart/form-data" action="imagetestprocess.php" method="post">
<input type="file" name="image">
<input type="submit" value="Submit">
</form>
表单处理程序..
<?php
include("../mylibrary/login.php");
login();
$imagefile = file_get_contents($_FILES['image']['tmp_name']);
$imagefile = mysql_real_escape_string($imagefile);
$query="UPDATE pieces SET image_full='$imagefile' WHERE assetno='1'";
$result = mysql_query($query);
?>
图像显示器..
<?php
include("../mylibrary/login.php");
login();
echo "<body>";
echo "before";
echo "<img src=\"showimage.php\" alt=\"showimage\">";
echo "after";
?>
调用的函数...
<?php
include("../mylibrary/login.php");
login();
$query = "select * from pieces where assetno='1'";
$result=mysql_query($query);
$row=mysql_fetch_array($result, MYSQL_ASSOC);
$image=$row['image_full'];
header("Content-type: image/jpeg");
echo $image;
?>
答
变化image_full字段类型MEDIUMBLOB/BLOB
用户这个$image = chunk_split(base64_encode(file_get_contents("image.jpg")));
的$imagefile = file_get_contents($_FILES['image']['tmp_name']);
代替和在显示图像功能使用图像如下。
header("Content-type: image/jpeg");
echo base64_decode($image);
答
使用函数mysql_escape_string或和addslashes和清除浏览器缓存,看看它是否工作
答
如果以上方法不为你工作。 尝试增加数据库中字段的长度。 如果它仍然不起作用, 您可以检查图像格式是RGB还是CMYK。格式应该是RGB在屏幕上看到的。 为了确保您可以尝试在浏览器中打开相同的图像文件。
答
我认为这与你的数据库编码有关。某些编码不支持二进制数据。
如果你不能改变编码,也许你保存和解码它之前,css base64编码数据显示时。唯一的事情是base64会增加3的大小。
嗨,谢谢你的回复。 image_full字段已经是mediumblob。 Did you mean .. $ imagefile = chunk_split(base64_encode(file_get_contents($ _ FILES ['image'] ['tmp_name']))); 恐怕没有什么区别。 – Paul 2012-02-09 10:03:42
midiumblob很好。并且编码会对您的file_get_content进行编码,因此您不会遇到垃圾字符问题.chunk_split会将字符串拆分为更小的块,以便在字符串较大时阅读图像内容。 – 2012-02-09 11:46:56