2016-09-18 730 views
-1

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

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

那麼一些構成網頁的HTML將是一個好開始 – RiggsFolly

+0

歡迎來到SO。 請閱讀[我可以問哪些主題](http://stackoverflow.com/help/on-topic) 和[如何提出一個好問題](http://stackoverflow.com/help/how-to - 問) 和[完美的問題](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) 以及如何創建[最小,完整和可驗證的例子] (http://stackoverflow.com/help/mcve) SO是**不是一個免費的編碼或代碼轉換或調試或教程或庫查找服務** 您還必須表明,你已經努力解決你自己的問題。 – RiggsFolly

+0

@RiggsFolly - 請你詳細一點。我不明白你在問什麼。 – Mathak

回答

0

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

其中Customer_image是一滴 功能GETCUSTOMER ($用戶)是爲客戶的所有信息

我上傳我用這個

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); 
}