簡體   English   中英

努力顯示與PHP Blob圖像

[英]Struggling to display blob image with php

我正在建立一個簡單的網站,我想允許用戶上傳和更改他們的頭像。 目前,我已經能夠將圖像上傳到mysql數據庫,並以blob形式存儲,其代碼如下:

//connected to DB, userID fetched

$image = $FILES['fileToUpload']['tmp_name'];
$fp = fopen($image, 'r');
$content = fread($fp, filesize($image));
$content = addslashes($content);
fclose($fp);

$sql = "UPDATE tbUsers SET profileImage = '".$content."' WHERE userID = ".userID;
$result = mysql_query($sql) or die (mysql_error());

上載后從phpmyadmin下載文件時,它們保存為.bin文件,但可以正常查看。 我不確定這是否正確。 我顯示圖像的代碼如下:

HTML:

<?php echo '<img src ="showPic.php?q='.$_SESSION['profile'].'"/>'; ?>

PHP:

if (!empty($_GET['profile']) && is_numeric($_GET['profile']))
{
  $con = mysql_connect("localhost", "root", "");
  $mysql_select_db("projectDB");
  $sql = "SELECT profileImage FROM tbUsers WHERE userID = ". $_GET['profile'];
  $result = mysql_query($sql) or die (mysql_error());

  header('Content-type: image/jpeg');
  $row = mysql_fetch_object($result);
  echo $row['image_data'];
}

我不確定是否要嘗試以正確的方式顯示圖像,將不勝感激任何幫助(更正/替代解決方案):)

你可以這樣做 :

if (!empty($_GET['profile']) && is_numeric($_GET['profile']))
{
  $con = mysql_connect("localhost", "root", "");
  $mysql_select_db("projectDB");
  $sql = "SELECT profileImage FROM tbUsers WHERE userID = ". $_GET['profile'];
  $result = mysql_query($sql) or die (mysql_error());

  $content = mysql_result($result,0,"file_content");
  $name = mysql_result($result,0,"file_name");
  $type = mysql_result($result,0,"file_type");
  $size = mysql_result($result,0,"file_size");
  header("Content-type: $type");

  echo $content

}

注意:您應該在表中具有這些列,用於保存BLOB數據

file_name =用於保存文件名

$_FILES['file']['name']

file_type =用於保存文件類型

$_FILES['file']['type']

file_size =用於保存文件大小

$_FILES['file']['size']

您選擇這個

$sql = "SELECT profileImage FROM tbUsers WHERE userID = ". $_GET['profile'];

並引用未選中的列

echo $row['image_data'];

暫無
暫無

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

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