简体   繁体   English

使用php获取BLOB图像并在html中显示

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

I am facing an issue getting an image from mysql database and displaying on the web page.我在从 mysql 数据库获取图像并在网页上显示时遇到问题。 I have a user_image table with userid, image (blob datatype), caption, imagename(name of the image, example, 0101.jpg), time(timestamp of the image upload).我有一个 user_image 表,其中包含用户 ID、图像(blob 数据类型)、标题、图像名称(图像名称,例如 0101.jpg)、时间(图像上传的时间戳)。 Users can upload image to database and view their images acc to the timestamp.用户可以将图像上传到数据库并根据时间戳查看他们的图像。 Any inputs on how to display the image on web page ?关于如何在网页上显示图像的任何输入? Here is the code:这是代码:

<?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();
?>

Here is the code for uploading image into database:这是将图像上传到数据库的代码:

<?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();
?>

i use this for my own site echo '<img src="data:image/jpeg;base64,'.base64_encode($customer->GetCustomer($user)["Customer_image"]).'">'我将它用于我自己的网站echo '<img src="data:image/jpeg;base64,'.base64_encode($customer->GetCustomer($user)["Customer_image"]).'">'

where Customer_image is blob the function GetCustomer($user) is for all information of customer其中Customer_image是 blob 函数 GetCustomer($user) 用于客户的所有信息

for my upload i use this对于我的上传,我使用这个

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