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

これを試して

        <?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 に答える
0

imagesディレクトリなしでDBに保存しています。それを保存するか、イメージ呼び出しで常にそのように呼び出すことを忘れないでください。

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

または、書き込み中のレコードをファイルシステムの場所と同じにします。

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

このコードを使用すると、SQL インジェクションとファイル インクルージョン インジェクションを受け入れることに注意してください。

于 2016-12-02T07:04:09.490 に答える