2

我尝试将图像上传到 mysql 数据库并使用 php 将其与图像描述一起显示。在我上传图像并显示后,显示了一个损坏的图像,但图像的描述显示没有任何错误。我怎么解决这个问题 ?感谢你的帮助

<?php

    $msg = "";
    //if upload button is pressed
    if(isset($_POST['upload']))     
    {
        // the path to store the uploaded image
        $target = "images/".basename($_FILES['image']['name']);

        // connect to database
        $db = mysqli_connect("localhost","root","","product");

        // Get all the submitted data from the form
        $image = $_FILES['image']['name'];
        $text = $_POST['text'];

        $sql = "INSERT INTO product_list (image, text) VALUES ('$image','$text')";
        mysqli_query($db,$sql); // stores the submitted data into the database table : product_list

        // move uploaded image to the folder : image
        if (move_uploaded_file($_FILES['image']['tmp_name'],$target))
        {
            $msg = "Image and text uploaded successfully";
        }else
        {
            $msg = "There was a problem uploading image";
        }
    }

?>

<!DOCTYPE html>
<html>
<head>
<title>Image Upload With Description</title>
<link rel="stylesheet" type="text/css" href="formstyle.css">
</head>
<body>
<div id="content">
<?php
    $db = mysqli_connect("localhost","root","","product");
    $sql = "SELECT * FROM product_list";
    $result = mysqli_query($db, $sql);
    while ($row = mysqli_fetch_array($result))
    {
        echo "<div id='img_div'>";
            echo "<img src='".$row['image']."'>";
            echo "<p>".$row['text']."</p>";
        echo "</div>";
    }
?>
    <form method="post" action="try.php" enctype="multipart/form-data">
        <input type="hidden" name="size" value="1000000">
        <div>
            <input type="file" name="image">
        </div>

        <div>
            <textarea name="text" cols="40" rows="4" placeholder="Details of product"></textarea>
        </div>

        <div>
            <input type="submit" name="upload" value="Upload Image">
        </div>
    </form>
</div>
</body>
</html>

这是我的结果:

在此处输入图像描述

4

2 回答 2

0

您将其存储在没有images目录的数据库中。您要么需要将其存储起来,要么始终记住在图像调用中以这种方式调用它。

echo "<img src='images/".$row['image']."'>";

或使您正在写入的记录与文件系统位置相同。

$image = 'images/' . $_FILES['image']['name'];

请注意,您可以使用此代码进行 SQL 注入和文件包含注入。

于 2016-12-02T07:04:09.490 回答
0

尝试这个

        <?php

            $msg = "";
            //if upload button is pressed
            if(isset($_POST['upload']))     
            {
                // the path to store the uploaded image
                $destination_path = getcwd().DIRECTORY_SEPARATOR;
                $target_path = $destination_path . basename( $_FILES["image"]["name"]);

                // connect to database
                $db = mysqli_connect("localhost","root","","product");

                // Get all the submitted data from the form
                $image = $_FILES['image']['name'];
                $text = $_POST['text'];

                $sql = "INSERT INTO product_list (image, text) VALUES ('$image','$text')";
                mysqli_query($db,$sql); // stores the submitted data into the database table : product_list


                //@move_uploaded_file($_FILES['image']['tmp_name'], $target_path)

                // move uploaded image to the folder : image
                if (move_uploaded_file($_FILES['image']['tmp_name'],$target_path))
                {
                    $msg = "Image and text uploaded successfully";
                }else
                {
                    $msg = "There was a problem uploading image";
                }
            }

        ?>

        <!DOCTYPE html>
        <html>
        <head>
        <title>Image Upload With Description</title>
        <link rel="stylesheet" type="text/css" href="formstyle.css">
        </head>
        <body>
        <div id="content">
        <?php
            $db = mysqli_connect("localhost","root","","product");
            $sql = "SELECT * FROM product_list";
            $result = mysqli_query($db, $sql);
            while ($row = mysqli_fetch_array($result))
            {
                echo "<div id='img_div'>";
                    echo "<img src='".$row['image']."'>";
                    echo "<p>".$row['text']."</p>";
                echo "</div>";
            }
        ?>
            <form method="post" action="index.php" enctype="multipart/form-data">
                <input type="hidden" name="size" value="1000000">
                <div>
                    <input type="file" name="image">
                </div>

                <div>
                    <textarea name="text" cols="40" rows="4" placeholder="Details of product"></textarea>
                </div>

                <div>
                    <input type="submit" name="upload" value="Upload Image">
                </div>
            </form>
        </div>
        </body>
        </html>
于 2016-12-02T07:05:08.913 回答