0

我正在学习php和mysqli。我编写了一个程序,允许用户将名称和图像上传到数据库。他们可以单击数据库中的姓名,然后单击姓名,他们将看到该人的照片。

下面是我的 PHP 文件。一切正常,只是图像不显示。当我点击一个名字时,我看到的只是一个带有问号的蓝色框。有人可以看看我的代码并给我一些关于如何解决这个问题的提示吗?谢谢!

主文件

    <?php

    //Turn on error reporting
    ini_set('display_errors', 'On');
    //Connects to the database
    $mysqli = new mysqli("abc", "abc","abc", "abc");
    if($mysqli->connect_errno){
        echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }
?>  


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
        <script>$(document).ready(function() {$("form").validate();});</script>
        <title>Homework</title>
        <meta charset="UTF-8">
        <p></p>
    </head>
    <body>
        <div>Homework</div>
        <p></p>
        <div>
            <form method="post" action="index.php" enctype="multipart/form-data">
                Celebrity Name: <input type="text" name="c_name">
                Celebrity Photo: <input type="file" name="c_picture">
                <input type="submit" name="add" value="Upload">
            </form>
        </div>
        <br>
        <div>
        <table>
        <tr>
            <td>See below for a list of celebrities in our database</td>
        </tr>
        <?php
            //Display names in the celebrity database
            if(!($stmt = $mysqli->prepare("SELECT c_id, c_name FROM celebrity"))){
                echo "Prepare failed: "  . $stmt->errno . " " . $stmt->error;
            }

            if(!$stmt->execute()){
                echo "Execute failed: "  . $mysqli->connect_errno . " " . $mysqli->connect_error;
            }

            if(!$stmt->bind_result($c_id, $c_name)){
                echo "Bind failed: "  . $mysqli->connect_errno . " " . $mysqli->connect_error;
            }

            while($stmt->fetch()){
                echo "<tr><td><a href='img.php?c_id=".$c_id."'>" . $c_name . "</a></td></tr>";
                //echo "<tr>\n<td>\n" . $c_name . "\n</td>\n</tr>";

            }

            $stmt->close();
        ?>
        </table>
        </div>
    </body>
</html>

img.php

    <?php

    //Turn on error reporting
    ini_set('display_errors', 'On');
    //Connects to the database
    $mysqli = new mysqli("blah", "blah","blah", "blah");
    if($mysqli->connect_errno){
        echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }

    $c_id=1;
    //retrieve the blob
    if(!($stmt = $mysqli->prepare("SELECT c_picture FROM celebrity where c_name = ?"))){
        echo "Prepare failed: "  . $stmt->errno . " " . $stmt->error;
    }

    //if(!($stmt->bind_param("i",$_GET['c_id']))){
    if(!($stmt->bind_param("i",$c_id))){
        echo "Bind failed: "  . $stmt->errno . " " . $stmt->error;
    }

    if(!$stmt->execute()){
        echo "Execute failed: "  . $mysqli->connect_errno . " " . $mysqli->connect_error;
    }

    $stmt->store_result();

    if(!$stmt->bind_result($c_picture)){
        echo "Bind failed: "  . $mysqli->connect_errno . " " . $mysqli->connect_error;
    }

    $stmt->fetch();
    header("Content-Type: image/jpeg");
    echo $c_picture;

    $stmt->close();
?>  

索引.php

    <?php

    //Turn on error reporting
    ini_set('display_errors', 'On');
    //Connects to the database
    $mysqli = new mysqli("blah", "blah","blah", "blah");
    if($mysqli->connect_errno){
        echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }

    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        $errorinfo = $_FILES['c_picture']['error'];
        $img = $_FILES['c_picture']['name'];
        $imgTmp = $_FILES['c_picture']['tmp_name'];
        $imgSize = $_FILES['c_picture']['size'];
        $imgType = $_FILES['c_picture']['type'];
    }

    //Check image type and size
    if (($imgType == 'image/jpeg' || $imgType == 'image/gif' || $imgType == 'image/png') && $imgSize < 1048576)
    {
        //Insert name and image file into celebrity database (c_name, c_picture) values (?, ?)
        if(!($stmt = $mysqli->prepare("INSERT INTO celebrity(c_name, c_picture) VALUES (?, ?)")))
        {
            echo "Prepare failed: "  . $stmt->errno . " " . $stmt->error;
        }


        if(!($stmt->bind_param("sb",$_POST['c_name'], $img)))
        {
            echo "Bind failed: "  . $stmt->errno . " " . $stmt->error;
        }


        if(!$stmt->execute())
        {
            echo "Execute failed: "  . $stmt->errno . " " . $stmt->error;
        }   
        else 
        {
            echo "Added " . $stmt->affected_rows . " celebrity to database.";
        }

    }
    else
    {
        echo "Only jpegs, gifs, and pngs under 1MB can be uploaded";
    }
?>  
4

3 回答 3

2

如果您打印,则插入时不会保存图像数据:

$img = $_FILES['c_picture']['name'];
echo $img;

它只是图像的原始名称,而不是您需要保存文件数据。如果您只想直接输出图像:

header("Content-Type: image/jpeg");
echo file_get_contents($_FILES['c_picture']['tmp_name']);

您需要做的是从$_FILES['c_picture']['tmp_name']数据库中获取图像数据并将其存储为 BLOB 或 LONGBLOB。我看到你已经在使用 BLOB,所以你只需要更改:

$img = $_FILES['c_picture']['name'];

相反,使用:

$img = file_get_contents($_FILES['c_picture']['tmp_name']);

此外,您的标题应该输出正确的图像类型,也许您也可以将其保存到数据库中。

在现实世界的应用程序中,您不应该将图像的二进制文件存储在数据库中,更好的方法是将图像存储在文件夹中,然后将地址保存在数据库中。

于 2013-08-02T00:03:39.460 回答
0

在 main.php 中显示图片的页面的链接是img.php

<a href='img.php?c_id=".$c_id."'>

然而,您在此处发布的显示图片的文件名称是 viewfile.php,因此只需将链接更改为指向 viewfile.php:

<a href='viewfile.php?c_id=".$c_id."'>
于 2013-08-01T22:12:45.400 回答
0

只是检查一下,如果你在数据库中输入了完整的图像标签,那么就会出现问题,但是如果你只是将图像的名称输入到数据库中,要查看它,你应该这样做

<img src="images/$c_name" alt="$c_name">

而不仅仅是 $c_name。

于 2013-08-01T22:25:18.337 回答