簡體   English   中英

將圖片上傳到數據庫並將路徑發送到數據庫

[英]upload picture to and send path to database

我正在嘗試上傳圖片並將其鏈接到我的數據庫,並且我使用了以下代碼:

Upload3.php

        <?php
    // Check if a file has been uploaded
    if(isset($_FILES['uploaded_file'])) {
        // Make sure the file was sent without errors
        if($_FILES['uploaded_file']['error'] == 0) {
            // Connect to the database
            $dbLink = new mysqli('localhost', 'root', '1234', 'fyp');
            if(mysqli_connect_errno()) {
                die("MySQL connection failed: ". mysqli_connect_error());
            }

            // Gather all required data
            $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
            $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
            $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
            $size = intval($_FILES['uploaded_file']['size']);

            // Create the SQL query
            $query = "
                INSERT INTO `pic` (
                    `Name`, `mime`, `size`, `data`, `created`
                )
                VALUES (
                    '{$name}', '{$mime}', {$size}, '{$data}', NOW()
                )";

            // Execute the query
            $result = $dbLink->query($query);

            // Check if it was successfull
            if($result) {
                echo 'Success! Your file was successfully added!';
            }
            else {
                echo 'Error! Failed to insert the file'
                   . "<pre>{$dbLink->error}</pre>";
            }
        }
        else {
            echo 'An error accured while the file was being uploaded. '
               . 'Error code: '. intval($_FILES['uploaded_file']['error']);
        }

        // Close the mysql connection
        $dbLink->close();
    }
    else {
        echo 'Error! A file was not sent!';
    }

        // Echo a link back to the main page
        echo '<p>Click <a href="index.html">here</a> to go back</p>';


 ?>

這是form.php

<html>
<body>

<form action="upload3.php" method="post"
enctype="multipart/form-data">
<label for="uploaded_file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

我到了最后一個階段,它的給出錯誤文件未發送

我不知道我在哪里錯過。

謝謝

將您的表單文件名更改為“ uploaded_file”。 您正在腳本中查找名稱為“ file”而不是“ uploaded_file”的不存在的文件

檢查文件名: Upload2.phpupload3.php (注意區分大小寫)。 name="file"更改為name="uploaded_file"

form.php

<html>
<body>
<form action="upload2.php" method="post" enctype="multipart/form-data">
    <label for="uploaded_file">Filename:</label>
    <input type="file" name="uploaded_file" id="uploaded_file"><br>
    <input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

upload2.php ,您忘記了mime字段:

$datetime = date("Y-m-d h:i:s");

// Create the SQL query
$query = "
    INSERT INTO `pic` (
        `Name`, `mime`, `size`, `data`, `created`
    )
    VALUES (
        '{$name}', '{$mime}', {$size}, '{$data}', '{$datetime}'
    )";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM