嘿,我在 php 中显示图像时遇到问题。图像存储在 mysql 的“images”表中。还有另一个表“餐厅”需要获取这些图像并根据restid显示相应的图像。然而,它面临着获取图像而不显示图像的问题。请帮忙! 这是imageupload.php:
<?php
require 'connect.inc.php';
?>
<html>
<head>
<title>Uploading image</title>
</head>
<body>
<?php
echo "<form action='imageupload.php' method='POST' enctype='multipart/form-data'>
Upload: <input type='file' name='image'><input type='submit' value='Upload' >
</form>";
if(isset($_FILES['image']['tmp_name']))
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size==FALSE)
echo "That's not an image";
else
{
$query = "INSERT INTO images VALUES ('','$image_name','$image','22')";
$result = mysqli_query($con, $query);
if(!$result)
{
echo "Problem uploading";
}
else
{
echo "Image uploaded ";
$query2 = "SELECT * FROM images WHERE restid = '22'";
$result2 = mysqli_query($con,$query2);
while($info = mysqli_fetch_array($result2))
{
header("Content-type: image/jpeg");
echo $info['image'];
}
}
}
}
else
{
"Please upload a file";
}
?>
</body></html>
这是 getimage.php(它获取图像并显示它):
<?php
require 'connect.inc.php';
$id = $_REQUEST['id'];
$image = "SELECT * FROM images WHERE imgid = $id" ;
$image = mysqli_query($con, $image);
$image = mysqli_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
connect.inc.php是连接数据库的文件。我引用了其他链接,但没有得到任何可靠的帮助。请提供帮助。
最佳答案
在 mysql 中存储图像应该可以。 检查是否有任何语法错误。 暂时删除 Content-type header 以查看图像文件是否被打印(作为乱码字符串)。还要检查您存储图像的 mysql 字段是否为 BLOB 类型。 如果您有任何错误,请发布。
关于php - 在 php mysql 中显示图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18946481/