2017-05-06 260 views
0

我的程序需要上传图片,因此图片(varchar)位置保存在MySQL数据库中。它工作到目前为止。如何使用PHP显示来自数据库的图像?

现在我想要显示图像,这是行不通的。下面是代码:

include ('connect.php'); 
if(isset($_POST['submit'])){ 
    $filetemp= $_FILES['image'] ['tmp_name']; 
    $filename= $_FILES['image'] ['name']; 
    $filepath= "images/".$filename; 

    move_uploaded_file($filetemp,$filepath); 
    $sql=mysqli_query($con,"insert into images (image) value ('$filepath')"); 
    if($sql){ 
     echo "uploaded"; 
    } 
    else{ 
     echo " not uploaded"; 
    } 
} 

$sql=mysqli_query($con,"select * from images"); 
while($row=mysqli_fetch_array($sql)){ 
    echo "<img src=' images/".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image 
} 

?> 
+0

什么是存储在'图像'在数据库? 'img'标签中的路径在'images /' – RamRaider

+0

之前有一个前导空格,这是保存在图像列“images/download.jpg”中的值。 –

回答

0

更正:

$sql=mysqli_query($con,"select * from images"); 
while($row=mysqli_fetch_array($sql)){ 
    echo "<img src='".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image 
} 

你也需要在必要时通过__FILE__获得的情况下,实际的路径。

2

从图像路径中删除images,你已经在imageimages表存储这种

echo "<img src='/".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image 
+0

echo“”; //你的代码有'/' 以下的行是正确的 echo“”; 谢谢 –

0

您添加图像/到数据库中,然后当你调用该行显示您要添加图像的图像/添加使它看起来在图像/图像/文件名

相关问题