簡體   English   中英

使用php獲取BLOB圖像並在html中顯示

[英]Getting BLOB image and display in html using php

我在從 mysql 數據庫獲取圖像並在網頁上顯示時遇到問題。 我有一個 user_image 表,其中包含用戶 ID、圖像(blob 數據類型)、標題、圖像名稱(圖像名稱,例如 0101.jpg)、時間(圖像上傳的時間戳)。 用戶可以將圖像上傳到數據庫並根據時間戳查看他們的圖像。 關於如何在網頁上顯示圖像的任何輸入? 這是代碼:

<?php
session_start();
$con = mysqli_connect("localhost", "root", "","login") or die("Unable to connect to MySQL");
$userid = $_SESSION['userid'];
$sql = "SELECT image,image_name,caption FROM user_image WHERE userid=$userid";
if($query = mysqli_query($con, $sql)) {
    while($row=mysqli_fetch_array($query)) {
        header('Content-type: image/jpeg');
        echo $row["image"];
    }
}else{
    echo "Error";
}
$con->close();
?>

這是將圖像上傳到數據庫的代碼:

<?php
session_start(); 
if (isset($_POST['submit'])){
echo $_SESSION["userid"];
$userid = $_SESSION["userid"];
$con=mysqli_connect("localhost","root","","login")or die("Unable to connect to MySQL");
 $caption = $_POST['Caption'];


 $imageName = mysqli_real_escape_string($con,$_FILES["fileToUpload"]["name"]);
$imageData = mysqli_real_escape_string($con,file_get_contents($_FILES["fileToUpload"]["tmp_name"]));
$imageType  = mysqli_real_escape_string($con,$_FILES["fileToUpload"]["type"]);
echo $imageName;
echo $imageType;
if(substr($imageType,0,5) == "image"){
$stmt = mysqli_prepare($con, "INSERT INTO user_image (userid,timestamp, image, image_name,caption) VALUES (?,now(), ?, ?,?)");
if ($stmt === false) {
trigger_error('Statement failed! ' . htmlspecialchars(mysqli_error($con)), E_USER_ERROR);
}
$bind = mysqli_stmt_bind_param($stmt, "isss", $userid,$imageData, $imageName, $caption);
if ($bind === false) {
trigger_error('Bind param failed!', E_USER_ERROR);}
$exec = mysqli_stmt_execute($stmt);
if ($exec === false) {
trigger_error('Statement execute failed! ' . htmlspecialchars(mysqli_stmt_error($stmt)), E_USER_ERROR); }
 }
else
{
echo " only images are allowed";}
}
$con->close();
?>

我將它用於我自己的網站echo '<img src="data:image/jpeg;base64,'.base64_encode($customer->GetCustomer($user)["Customer_image"]).'">'

其中Customer_image是 blob 函數 GetCustomer($user) 用於客戶的所有信息

對於我的上傳,我使用這個

if(isset($_POST["send"])){
    $tmpName = $_FILES['image']['tmp_name'];
    $name = $_FILES['image']['name'];
    $fp = fopen($tmpName, 'r');
    $data = fread($fp, filesize($tmpName));
    fclose($fp);
    $product->UploadImageProduct($id, $name, $data);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM