这是我的.php文件
<?php
ini_set('mysql.connect_timeout',300);
ini_set('default_socket_timeout',300);
?>
<html>
<body>
<form method="post" enctype="multipart/form-data">
<br/>
<input type="file" name="image" />
<br/><br/>
<input type="submit" name="sumit" value="Upload" />
</form>
<?php
if(isset($_POST['sumit']))
{
if(getimagesize($_FILES['image']['tmp_name']) == FALSE)
{
echo "Please select an image.";
}
else
{
$image= addslashes($_FILES['image']['tmp_name']);
$image= file_get_contents($image);
$image= base64_encode($image);
saveimage($image);
}
}
displayimage();
function saveimage($image)
{
$con=mysql_connect("localhost","root","");
mysql_select_db("food",$con);
$qry="insert into info (image) values ('$image')";
$result=mysql_query($qry,$con);
if($result)
{
echo "<br/>Image uploaded.";
}
else
{
echo "<br/>Image not uploaded.";
}
}
function displayimage()
{
$con=mysql_connect("localhost","root","");
mysql_select_db("food",$con);
$qry="select image from info";
$result=mysql_query($qry,$con);
while($row = mysql_fetch_assoc($result))
{
echo '<img height="300" width="300" src="data:image;base64,'.$row[2].' "> ';
}
mysql_close($con);
}
?>
</body>
</html>为什么我不能显示我的图片当我添加它。当我点击上传时,图片已经存储在数据库中了,但是我不能显示它。那里的显示代码有什么问题吗?谢谢
发布于 2016-05-19 08:32:13
看这条线,
while($row = mysql_fetch_assoc($result)){ ...您将行作为关联数组(而不是数字数组)获取,因此$row[2]将无法工作。在您的$row['image']元素中使用<img>来显示图像。
所以你的代码应该是这样的:
while($row = mysql_fetch_assoc($result)){
echo '<img height="300" width="300" src="data:image;base64,'. $row['image'] . '">';
}Sidenotes:
mysql_*函数,它们在PHP5.5中被废弃,在PHP7.0中被完全删除。使用mysqli或pdo代替。函数。发布于 2016-05-19 08:31:00
在当前代码中,此处更改
将$row[2]替换为$row['image']
并尝试按照注释的建议转换代码。
https://stackoverflow.com/questions/37317544
复制相似问题