【问题标题】:Images display blank when retrieving from database php mysql从数据库 php mysql 检索时图像显示空白
【发布时间】:2014-10-30 21:21:35
【问题描述】:

我有四个文件:

  1. main.php我的html提交表单,提交一个图片和带图片的文字

  2. storeinfo.php 它将我的所有数据从 html 表单发送到它工作的数据库,我的表单中的图像和文本已成功提交

  3. image.php 从数据库中获取图像,并具有一个标头函数,用于将图像类型转换为图像为 png、jpeg 等的任何格式。

  4. show.php 获取与图像一起发布的所有文本并显示所有带有文本的图像,但是图像不显示,而是在图像无法显示时得到一个空白框。

我找不到我的错误,我猜它与 image.php 中的 header 函数有关,或者当我尝试在 show.php 中显示带有 html img 标签的图像时。 将图片(存储为blob)上传到数据库成功。 为什么不显示图片?

代码按每页排序:

  1. main.php html 表单

    <form enctype="multipart/form-data" action="storeinfo.php" method="POST">
    
    <table border=0 align=center bgcolor=black width=100%>
    <tr><td colspan=2><h2>&nbsp</h2></td></tr>
    </table>
    
    
    <table border=0 align=center bgcolor=grey>
    <tr><td colspan=2><h2>Animal Information</h2></td></tr>
    <tr>
    <td>Name</td><td><input type=text name="aname"></td>
    </tr>
    <tr>
    <td>Description</td><td><input type=text name="adetails"></td>
    </tr>
    <tr>
    <td>Photo</td><td><input type=file name="aphoto"></td>
    </tr>
    <tr>
    <td></td><td><input type=submit name="submit" value="Store Information"></td>
    </tr>
    </table>
    </form>
    
  2. storeinfo.php

    <?php
    $conn = mysql_connect("localhost","root","");
    if(!$conn)
    {
    echo mysql_error();
    }
    $db = mysql_select_db("imagestore",$conn);
    if(!$db)
    {
    echo mysql_error();
    }
    $aname = $_POST['aname'];
    $adetails = $_POST['adetails'];
    $aphoto = addslashes (file_get_contents($_FILES['aphoto']['tmp_name']));
    $image = getimagesize($_FILES['aphoto']['tmp_name']);//to know about image type etc
    
    $imgtype = $image['mime'];
    
    $q ="INSERT INTO animaldata VALUES('','$aname','$adetails','$aphoto','$imgtype')";
    
    $r = mysql_query($q,$conn);
    if($r)
    {
    echo "Information stored successfully";
    }
    else
    {
    echo mysql_error();
    }
    ?>
    
  3. image.php

    <?php
    
    $conn = mysql_connect("localhost","root","");
    if(!$conn)
    {
    echo mysql_error();
    }
    $db = mysql_select_db("imagestore",$conn);
    if(!$db)
    {
    echo mysql_error();
    }
    $id = $_GET['id'];
    $q = "SELECT aphoto,aphototype FROM animaldata where id='$id'";
    $r = mysql_query("$q",$conn);
    if($r)
    {
    
    $row = mysql_fetch_array($r);
    $type = "Content-type: ".$row['aphototype'];
    header($type);
    echo $row['aphoto'];
    }
    else
    {
    echo mysql_error();
    }
    
    ?>
    
  4. 显示.php

    <?php
    //show information
    
    
    $conn = mysql_connect("localhost","root","");
    if(!$conn)
    {
    echo mysql_error();
    }
    $db = mysql_select_db("imagestore",$conn);
    if(!$db)
    {
    echo mysql_error();
    }
    
    $q = "SELECT * FROM animaldata";
    $r = mysql_query("$q",$conn);
    if($r)
    {
    while($row=mysql_fetch_array($r))
    {
    //header("Content-type: text/html");
    echo "</br>";
    echo $row['aname'];
    echo "</br>";
    echo $row['adetails'];
    echo "</br>";
    
    //$type = "Content-type: ".$row['aphototype'];
    //header($type);
    
     //$lastid = mysql_insert_id();
    // $lastid = $lastid;
     //echo "Your image:<br /><img src=image.php?id=$lastid />";
    
    echo "<img src=image.php?id=".$row['id']." width=300 height=100/>";
    
    
    }
    }
    else
    {
    echo mysql_error();
    }
    
    
    ?>
    

【问题讨论】:

  • 您使用什么类型的数据库存储内容?另外我不太确定你可以像这样将二进制内容保存到数据库中。
  • 首先你应该引用你的图像源字符串:src='image.php?id=".$row['id']."'。除此之外,您应该尝试缩小问题范围,对于开发人员工具的 net 选项卡中的图像、mysql 错误消息或图像内容,您会得到什么样的响应?
  • 我正在使用 id,aname varchar 200,详细文本,BLOB for aimage,aphototype varchar 200
  • 我没有收到任何错误消息,只是默认显示空白图像,就像没有显示图像时一样。

标签: php mysql image header fetch


【解决方案1】:

首先,我在这里找到了关于如何做你想做的事情的教程: http://www.mysqltutorial.org/php-mysql-blob/

第二个你应该使用 mysql_escape_string(file_get_contents($_FILES['aphoto']['tmp_name'])) 而不是 addshlashes。

根据这两条规则,您应该能够找出您的代码有什么问题,您也可以尝试使用较小的图片。

【讨论】:

    【解决方案2】:

    您的代码存在许多问题,但最值得注意的是您使用的是deprecated mysql functions,并且您的代码容易受到SQL injection attack 的攻击。

    我已重写 storeinfo.phpimage.php 以使用 mysqli 扩展并使用参数绑定来缓解 SQL 注入。我会把重写show.php 作为练习留给你。

    请注意,我对您的表结构做了一些假设,因此您可能需要对 SQL 代码进行一些调整。

    storeinfo.php

    $aname = $_POST['aname'];
    $adetails = $_POST['adetails'];
    $aphoto = file_get_contents($_FILES['aphoto']['tmp_name']);
    $image = getimagesize($_FILES['aphoto']['tmp_name']);//to know about image type etc
    $imgtype = $image['mime'];
    
    $conn = new mysqli("localhost","root","", "imagestore");
    if ($conn->connect_errno) {
        echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
    }
    
    if (!($stmt = $conn->prepare("INSERT INTO animaldata (aname, adetails, aphoto, aphototype) VALUES(?, ?, ?, ?)"))) {
        echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
    }
    if (!$stmt->bind_param("ssbs", $aname, $adetails, $aphoto, $imgtype)) {
        echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
    }
    $stmt->send_long_data(2, $aphoto);
    
    if (!$stmt->execute()) {
        echo "Insert failed: (" . $conn->errno . ") " . $conn->error;
    } else {
        echo "Information stored successfully";
    }
    

    image.php

    $conn = new mysqli("localhost","root","", "imagestore");
    if ($conn->connect_errno) {
        echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
    }
    
    if (!($stmt = $conn->prepare("SELECT aphoto, aphototype FROM animaldata where id=?"))) {
        echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
    }
    if (!$stmt->bind_param("i", $_GET['id'])) {
        echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
    }
    
    if (!$stmt->execute()) {
        echo "Select failed: (" . $conn->errno . ") " . $conn->error;
    } else {
        $stmt->bind_result($aphoto, $aphototype);
        $stmt->fetch();
    
        header("Content-type: ".$aphototype);
        echo $aphoto;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 2014-02-28
      • 2012-01-09
      • 2021-10-07
      相关资源
      最近更新 更多