我无法显示数据库中的图像

时间:2018-10-14 17:42:24

标签: php html mysqli

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

索引文件

$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');

照片将被保存在文件目标位置,以及数据库中的数据。除图像外,所有详细信息都将显示。

2 个答案:

答案 0 :(得分:0)

您需要选择图像数据并以正确的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类型确定哪种图像类型,然后使用诸如imagecreatefrompng之类的ImageMagick东西(),发送适当的内容标头,然后使用imagepng()

发送图像数据本身

答案 1 :(得分:0)

您是否检查过页面源代码并查看代码所调用的内容?

这对我来说似乎很可疑,因为您有数据库查询的图像结果,然后是在''

之外的一端进行了上传/标记
<div><img src='".$row['image']."' uploads/></div>

应该是:

<div><img src='uploads/".$row['image']."'></div>