使用存储在数据库中的图像路径获取图像

时间:2018-03-02 05:06:59

标签: php mysql

以下代码仅显示当前上传的图像。如何使用数据库中存储的路径获取所有图像

<?php
if(isset($_POST['submit']))
{
if($link)
{
 $filetemp=$_FILES['file']['tmp_name'];
 $filename=$_FILES['file']['name'];
  $filepath="uploaded/";
  $location=$path.$filename;
  move_uploaded_file($filetemp,$location);
  $query=mysqli_query($link,"call imageInsert('$filename','$filepath','$filetype')");
  if($query)
  {
    echo "Image inserted successfully...";
  }
  else
  {
   echo "Insertion Failedd!!!";
  }
   echo '<img width="250" height="250" src= "'.$location.'"/>';
}
}
 ?>

2 个答案:

答案 0 :(得分:0)

更改此

$filepath="uploaded/";
$location=$filepath.$filename; # its filepath not path

如果图片上传失败echo '<img width="250" height="250" src= "'.$location.'"/>';将无效。

if(move_uploaded_file($filetemp,$location))
{   
    # this `mysqli_query` not valuable to use on here, Because you're already in same function
    # $query=mysqli_query($link,"call imageInsert('$filename','$filepath','$filetype')");
    echo "Image inserted successfully...";
    echo '<img width="250" height="250" src= "'.$location.'"/>';
}
else
{
    echo "Insertion Failedd!!!";
}

答案 1 :(得分:0)

我使用下面的代码上传图像,保存相对路径 进入数据库并一起获取所有图像

     if(isset($_POST['submit']))
    { $host = "";
   $db_name = "";
     $username = "";
   $password = "";
$link=mysqli_connect($host, $username, $password, $db_name);
$filename=$_FILES['file']['name'];
$filetmp=$_FILES['file']['tmp_name'];
$target="uploaded/".$filename;
$image_url="http://holo-com.stackstaging.com/uploaded".$filename;
$sql="INSERT INTO images (name,image_url) VALUES   ('$filename','$image_url')";
 mysqli_query($link,$sql);
 if(move_uploaded_file($filetmp,$target)){
  echo "Image uploaded";
  }
  else
  {
  echo "Failed to upload";
   }
  }


?>

      and for fetching the images

      $link=mysqli_connect($host,$db_name,$password,$username);
  $sql="SELECT * FROM images ORDER BY id DESC ";
  $result=mysqli_query($link,$sql);


  while($row=mysqli_fetch_array($result))
 {
    $image[] = $row;
 }


  foreach($image as $brand){

    echo"<img src='uploaded/".$brand["name"]."'width=33%,height=33%>";

 }