簡體   English   中英

上傳到數據庫后如何解決使用php顯示的損壞圖像

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

我嘗試將圖像上傳到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>

這是我的結果:

在此處輸入圖片說明

您將其存儲在沒有images目錄的數據庫中。 您要么需要用它來存儲它,要么總是記得在圖像調用中以這種方式來調用它。

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

或使您要記錄的記錄與文件系統位置相同。

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

請注意,您可以使用此代碼進行SQL注入和文件包含注入。

嘗試這個

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