首页 > 解决方案 > 我无法显示数据库中的图像

问题描述

我正在尝试创建一个在线购物系统,该系统将显示和添加带有图像的产品。我已经观看并按照视频教程进行操作,但我似乎无法显示数据库中的图像。

索引文件

$sql = "SELECT * FROM tblproducts ORDER BY placeOrder DESC";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt , $sql)) {
  echo "SQL statement failed!";
} else {
  mysqli_stmt_execute($stmt);
  $result = mysqli_stmt_get_result($stmt);

  while ($row = mysqli_fetch_assoc($result)){

    echo"
   <div><img src='".$row['image']."' uploads/></div>
    <h3>".$row["imageTitle"]."</h3>
    <p>".$row["prod_description"]."</p>";

  }
}

上传文件

    if (isset($_POST['submit'])) {

    $newFileName = $_POST['filename'];
    if (empty($newFileName)) {
    $newFileName = "tblproducts";
    } else {
    $newFileName = strtolower(str_replace(" ","-", $newFileName));
    }
    $imageTitle = $_POST['filetitle'];
    $imageDesc = $_POST['filedesc'];

    $file = $_FILES['file'];

    $fileName = $file["name"];
      $fileType = $file["type"];
      $fileTempName = $file["tmp_name"];
      $fileError = $file["error"];
      $fileSize = $file["size"];

  $fileExt = explode(".", $fileName);
  $fileActualExt = strtolower(end($fileExt));

  $allowed = array("jpg", "jpeg", "png");

  if (in_array($fileActualExt, $allowed)) {
    if ($fileError === 0) {
      if ($fileSize < 2000000) {
      $imageFullName = $newFileName . "." . uniqid("", true) . "." . $fileActualExt;
      $fileDestination = "uploads/" . $imageFullName;


        include_once "conn.php";

        if (empty($imageTitle) || empty($imageDesc)) {
        header("Location: ../index.one.php?upload=empty");
        exit();
      }else{
          $sql = "SELECT * FROM tblproducts;";
          $stmt = mysqli_stmt_init($conn);
          if (!mysqli_stmt_prepare($stmt, $sql)) {
            echo "SQL statement failed! ";
          } else {
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);
            $rowCount = mysqli_num_rows($result);
            $setImageOrder = $rowCount + 1;

            $sql = "INSERT INTO tblproducts (imageTitle, prod_description, image, placeOrder) VALUES (?, ?, ?, ?);";
            if (!mysqli_stmt_prepare($stmt, $sql)) {
              echo "SQL statement failed! ";
            } else {
              mysqli_stmt_bind_param($stmt, "ssss",$imageTitle, $imageDesc, $imageFullName, $setImageOrder);
              mysqli_stmt_execute($stmt);

              move_uploaded_file($fileTempName, $fileDestination);

            header('location:index.one.php?productadded');

照片被保存在文件目的地和数据库中的数据中。除图像外,所有细节都在显示。

标签: phphtmlmysqli

解决方案


您需要选择图像数据并使用正确的 MIME 类型等发送出去。

这是我对文件附件执行的强制下载操作

<?php

// gives me executeQuery which does pdo prepared query and returns an array
// where [0] is true or false, and [1] is either error message (if 0 false)
// or [1..N] are the rows returned from a select
include('PDO.inc.php');

$q="SELECT file_data, file_mime, file_size, file_name FROM fileStorage where pk=?";
$a=array(33);
$res=executeQuery($q,$a);
if(!$res[0]){
    print("<h2><font color=red>Error</font> ".$res[1]."</h2>");
    exit;
}

header("Content-length: ".$res[1]['file_size']);
header("Content-type: ".$res[1]['file_type']);
header("Content-Disposition: attachment; filename=".$res[1]['file_name']);
echo $res[1]['file_data'];
?>

如果您想做诸如将图像嵌入网页而不强制“另存为...”之类的操作,您可以根据存储的 mime 类型确定图像类型,然后使用适当的 ImageMagick 东西,如imagecreatefrompng (),发送适当的内容头,然后用 imagepng ()发送图像数据本身


推荐阅读