简体   繁体   English

上传到数据库后如何解决使用php显示的损坏图像

[英]How to solve broken image displayed using php after upload to database

I try upload image to mysql database and display it along with the description of image using php. 我尝试将图像上传到mysql数据库,并使用php将其与图像描述一起显示。 After i upload the image and display it , a broken image was displayed but the description of the image was displayed without any error. 在我上传并显示图像后,显示了损坏的图像,但是显示了图像说明,没有任何错误。 How can I solve this problem ? 我怎么解决这个问题 ? Appreciate your help 感谢您的帮助

<?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>

This is my result : 这是我的结果:

在此处输入图片说明

You are storing it in the DB without the images directory. 您将其存储在没有images目录的数据库中。 You either need to store it with that, or always remember to call it that way in your image calls. 您要么需要用它来存储它,要么总是记得在图像调用中以这种方式来调用它。

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

or make your the record you are writing the same as the filesystem location. 或使您要记录的记录与文件系统位置相同。

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

Note you are open to SQL injections and file inclusion injections with this code. 请注意,您可以使用此代码进行SQL注入和文件包含注入。

try this 尝试这个

        <?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM