【问题标题】:Display image from path in database从数据库中的路径显示图像
【发布时间】:2015-02-12 19:45:48
【问题描述】:

我想显示我上传的图像并将图像的路径保存在 MySQL 数据库中。

以下是我用来上传图片和将路径保存到数据库中的表的所有代码:

HTML 表单:

<!DOCTYPE html>
<html>

<body>


<form action="processfangstrapport.php" enctype="multipart/form-data" method="post">

<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="billed1" type="file" accept="image/jpeg">
</td>
</tr>

<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>
</tbody></table>
</form>
</body>
</html>

我的进程php:

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "fiskerirapporter";

//Opretter forbindelse til databasen
$conn = new mysqli($servername, $username, $password, $dbname);
//Check forbindelse
if ($conn->connect_error) {
    die("Forbindelse mislykkedes: " . $conn->connect_error);
}

    function GetImageExtension($imagetype)
     {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }
     }

if (!empty($_FILES["billed1"]["name"])) {

    $file_name=$_FILES["billed1"]["name"];
    $temp_name=$_FILES["billed1"]["tmp_name"];
    $imgtype=$_FILES["billed1"]["type"];
    $ext= GetImageExtension($imgtype);
    $imagename=date("d-m-Y")."-".time().$ext;
    $target_path = "images/".$imagename;

if(move_uploaded_file($temp_name, $target_path)) {

    $query_upload="INSERT into images_tbl (images_path,submission_date) VALUES 

('".$target_path."','".date("Y-m-d")."')";
    mysqli_query($conn, $query_upload) or die("error in $query_upload == ----> ".mysql_error());  

}else{

   exit("Error While uploading image on the server");
} 
echo "Din fangstrapport er nu oprettet.";
}
$conn->close();
?>

以及我的数据库表的结构:

CREATE TABLE images_tbl(
id INT NOT NULL AUTO_INCREMENT unique,
images_path VARCHAR(200) NOT NULL,
submission_date DATE
);

【问题讨论】:

  • 你在 $target_path 中得到了正确的路径吗?
  • 您是否调试过代码以验证所有变量(例如@usermesam0023 提到的路径)是否正确?另外,你在这个过程中是否遇到任何错误,你检查过你的错误日志吗?
  • 你是说我上传图片的时候?上传到的路径和数据库中保存的路径是一样的。
  • 上传图片时没有出现任何错误。
  • 无论你是否看到任何错误,请确保在调用 move_uploaded_file() 之前,所有变量都有适当的值。

标签: php mysql image upload


【解决方案1】:

为此,您需要做的就是从数据库中获取您想要的路径,并在 HTML 图像标签src 中使用该路径:

echo '<img src="$imagePathFromDatabase" ... >';

要从数据库中获取图像,我们只需通过查询SELECT 它,类似于:

SELECT images_path FROM images_tbl WHERE images_path='imagePathYouWant' LIMIT 1;

【讨论】:

    【解决方案2】:

    将表数据加载到$result,并根据您的要求对以下查询应用修改。

        foreach ($result as $value){
         $path = 'http://address-upto-the-image-directory/'.$value[imagepath];
         echo "<img src='".$path."' />";
        }
    

    这里imagepath 是您在表中存储路径的字段名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      相关资源
      最近更新 更多