[英]Displaying an image in PHP using name stored in database
I am trying to display an image that is in a folder "upload" by getting the image name from the database. 我试图通过从数据库中获取图像名称来显示“上传”文件夹中的图像。
while($row = mysqli_fetch_array($result))
{
$pic = $row['image'];
echo $row['item'] ;
echo $row['location'];
echo $row['description'];
echo $row['forum'];
echo $row['datetime'];
echo $row['username'];
?>
</br>
<img src="upload/<?php echo $pic ?>"/>
<?php echo $row['image']; } ?>
"upload/<?php echo $pic ?>"
</body>
</html>
As you can see it display everything except the img src. 如您所见,它显示除img src之外的所有内容。
This is my database (ignore BLOB that was a test). 这是我的数据库(忽略测试的BLOB)。 I can't seem to figure out where I'm going wrong.
我似乎无法弄清楚我要去哪里。 Thanks
谢谢
My recommendation is to store the full relative path in the database like this: 我的建议是将完整的相对路径存储在数据库中,如下所示:
uploads/folder/file.jpg
My preferred MySQL field type is 'varchar(255)' the your echo in te PHP code will look like: 我首选的MySQL字段类型是'varchar(255)',您在PHP代码中的回显将类似于:
echo '<img src="'. $row['image'].'" />';
You are trying to print the image source outside the while-loop. 您正在尝试在while循环之外打印图像源。 The while-loop will only exit when
$row
is empty, so $row['image']
is also empty. while循环仅在
$row
为空时退出,因此$row['image']
也为空。
1) view source (or use firebug) to see the image tag and see what is the src given there. 1)查看源代码(或使用Firebug)查看图片标签,并查看此处给出的src。
2) try the url: "http://{LOCAL}/projects/projectviewposted.php/upload/happyball(1).jpg" and see if you can open the image 2)尝试网址:“ http:// {LOCAL} /projects/projectviewposted.php/upload/happyball(1).jpg”,看看是否可以打开图片
Try this 尝试这个
<img src="upload/<?php echo $pic; ?>"/>
<?php echo $row['image']; } ?>
"upload/<?php echo $pic ?>"
Else note down the link of the folder path. 否则记下文件夹路径的链接。 It might be that the folder path is not correct.
文件夹路径可能不正确。
Hope this helps 希望这可以帮助
You can try : 你可以试试 :
while($row = mysqli_fetch_array($result))
{
$pic = $row['image'];
echo $row['item'] ;
echo $row['location'];
echo $row['description'];
echo $row['forum'];
echo $row['datetime'];
echo $row['username'];
echo '<br />
<img src="upload/'.$pic.'"/>
'.$row['image'];
}
Well after 3 days I figured it out... my header location was incorrect! 好三天后,我发现了...我的标题位置不正确! ../project/projectviewposted.php
/
A stupid extra slash at the end!! ../project/projectviewposted.php
/
一个愚蠢的斜杠! Thanks for your help and suggestions! 感谢您的帮助和建议!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.