无法将通过 PHP 上传的图像显示到 MySQL 中
我已将代码精简到最低限度,试图找出为什么我无法显示我上传和下载的任何图像。通过 PHP 存储到 MySQL 中。如果有人能指出我的错误,我将不胜感激。
执行时,浏览器报告图像无法显示,因为它包含错误。
然而,图像上传&在同一环境中运行的其他数据库中显示良好。
我检查过上传后数据库是否保存有一个 blob。
我想我错过了一些明显的东西。
上传表单..
<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;
?>
I've trimmed my code down to the bare minimum to try to find why I cannot display any image that I upload & store via PHP into MySQL. If anyone can point out my error(s) I'd be most grateful.
On execution, the browser reports that the image cannot be displayed as it contains errors.
However, the image uploads & displays fine in other databases running in this same environment.
I've checked that the database holds a blob after upload.
I guess I'm missing something obvious.
Upload form..
<body>
<form enctype="multipart/form-data" action="imagetestprocess.php" method="post">
<input type="file" name="image">
<input type="submit" value="Submit">
</form>
Form handler..
<?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);
?>
Image displayer..
<?php
include("../mylibrary/login.php");
login();
echo "<body>";
echo "before";
echo "<img src=\"showimage.php\" alt=\"showimage\">";
echo "after";
?>
called function...
<?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;
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 image_full 字段类型更改为
MEDIUMBLOB / BLOB
用户此
$image = chunk_split(base64_encode(file_get_contents("image.jpg")));
而不是
$imagefile = file_get_contents($_FILES['image']['tmp_name']);
并在显示图像函数中使用图像,如下所示。
change the image_full field type to
MEDIUMBLOB / BLOB
user this
$image = chunk_split(base64_encode(file_get_contents("image.jpg")));
instead of
$imagefile = file_get_contents($_FILES['image']['tmp_name']);
and in show image function use image as below.
使用mysql_escape_string或addslashes并清除浏览器缓存以查看它是否有效
use mysql_escape_string or addslashes and clear your browser cache to see if it works
如果上述解决方案对您不起作用。
尝试增加数据库中字段的长度。
如果还是不行的话
您可以检查图像格式是 RGB 还是 CMYK。
格式应为 RGB 以在屏幕上看到。
为了确保您可以尝试在浏览器中打开相同的图像文件。
If the above solutions does not work for you.
Try increasing the length of the field in database.
if still it does not work,
You can check if the image format is RGB or CMYK.
format shoud be RGB to see on screen.
To make it sure you can try opening the same image file in browser.
我认为这与您的数据库编码有关。某些编码不支持二进制数据。
如果您无法更改编码,也许您可以在保存之前对数据进行 css base64 编码,并在显示时对其进行解码。唯一的问题是 base64 会将大小增加 3。
I think it has something to do with your database encoding. some encoding does not support binary data.
If you cannot change the encoding, maybe you css base64 encode the data before saving and decode it when displaying. only thing is base64 will increase the size by 3.