未从数据库接收图像

时间:2016-05-13 01:55:17

标签: php html mysql

存储在数据库中的所有其他信息都在网站上,而不是作为BLOB图像的图像。数据库保存图像,就像按下它一样,将显示正在存储的图像。任何赞赏!

GetImage.php

<?php
include "connection.php";

$ID = $_GET["ID"];

$SQLCommand = "SELECT * FROM products WHERE ID = '$ID'";
$Result = mysqli_query($con, $SQLCommand);

$row = mysqli_fetch_assoc($Result);

header("Content-type:" . $row["Name"]);
echo $row["Image"];

?>

shop.php

<?php
include "connection.php";

$SQLCommand = "SELECT * FROM products";
$Result = mysqli_query($con, $SQLCommand);

while($row = mysqli_fetch_assoc($Result)){
echo "<p id='p1'>" . $row["Name"] .
     "<br></p><p id='p3'>£" . $row["Price"] .
              "<br><br><img src=GetImage.php?ID=" . $row["ID"] . "         width=10%>" .
         "<br><a href=productdetails.php?ID=" . $row["ID"] . ">Details</a></p>".
         "<p id='p2'><a href=BasketAdd.php?ID=" . $row["ID"] . ">Add to     Basket</a><br><br>";
}
?>

InsertImageform

    <html>
<body>
<form action="UploadFile.php" method="post"

是enctype =&#34;多部分/格式数据&#34;&GT;

Name: <input type="text" name="name" id="name"><p>
Description: <input type="text" name="description" id="description"><p>
Price: <input type="text" name="price" id="price"><p>
Protein: <input type="text" name="protein" id="protein"><p>
Type: <input type="text" name="type" id="type"><p>

Filename: <input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

相反,你可以使用,

<?php
    include "connection.php";

    $SQLCommand = "SELECT * FROM products";
    $Result = mysqli_query($con, $SQLCommand);

    while($row = mysqli_fetch_assoc($Result)){
    echo "<p id='p1'>" . $row["Name"] .
         "<br></p><p id='p3'>£" . $row["Price"] .
                  "<br><br><img src=" . base64_encode($row['image']) . "         width=10%>" .
             "<br><a href=productdetails.php?ID=" . $row["ID"] . ">Details</a></p>".
             "<p id='p2'><a href=BasketAdd.php?ID=" . $row["ID"] . ">Add to     Basket</a><br><br>";
    }
?>

您应该使用base64_encode()函数来读取BLOB图像。

答案 1 :(得分:0)

数据库中图像的问题通常与图像存储在数据库中的方式有​​关。

图像可以存储为二进制文件,但如果存储为base64,则问题较少。

我需要看到的是将图像存储在数据库中的代码。
给定名称为&#34; image&#34;

的文件输入
circle.setPosition(x,y)

然后

<input type="file" name="image">

或作为更新:

if( is_uploaded_file($_FILES['image']['tmp_name']) || !($_FILES['image']['error'] !== UPLOAD_ERR_OK)){
   $img = base64_encode($_FILES['image']);
   $sql = "INSERT INTO `images`(`image`) VALUES('$img')";
   ...
 }

在表格中,字段图像的类型为&#34; TEXT&#34;

但仍然不够......

您需要知道图像的尺寸和类型。

UPDATE `table` SET `image`='$img' WHERE ...

获取宽度和高度

if( is_uploaded_file($_FILES['image1']['tmp_name']) || !($_FILES['image1']['error'] !== UPLOAD_ERR_OK)){
  $save = false;
  switch(strtolower($_FILES['image1']['type'])){
  case 'image/jpeg':
    $image = @imagecreatefromjpeg($_FILES['image']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/png':
    $image = @imagecreatefrompng($_FILES['image']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/gif':
    $image = @imagecreatefromgif($_FILES['image']['tmp_name']);
    if ($image !== false){$save = true;break;}
  default:
    $img = @getimagesize($_FILES['image']['tmp_name']);
    switch(strtolower($img['mime'])){
    case 'image/jpeg':
      $image = @imagecreatefromjpeg($_FILES['image']['tmp_name']);
      $type = 'jpg';
      if ($image !== false){$save = true;break;}
    case 'image/png':
      $image = @imagecreatefrompng($_FILES['image']['tmp_name']);
      $type = 'png';
      if ($image !== false){$save = true;break;}
    case 'image/gif':
      $image = @imagecreatefromgif($_FILES['image']['tmp_name']);
      $type = 'gif';
      if ($image !== false){$save = true;break;}
    default:
      $filename = $_FILES['image']['name'];
      $ext = substr($filename,-3);
      switch(strtolower($ext)){
      case 'jpg':
        $image = @imagecreatefromjpeg($_FILES['image']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'gif':
        $image = @imagecreatefromgif($_FILES['image']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'png':
        $image = @imagecreatefrompng($_FILES['image']['tmp_name']);
        if ($image !== false){$save = true;break;}
      default:
        $image = @imagecreatefromjpeg($_FILES['image']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefrompng($_FILES['image']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefromgif($_FILES['image']['tmp_name']);
        if ($image !== false){$save = true;break;}
      }
    }
  }
}

将所有内容放在一起,以获得HTML $width = imagesx($image); $height = imagesy($image); $base64 = base64_encode($image);

<img>