上传图片并发送到数据库的路径

时间:2014-05-27 21:12:00

标签: php file-upload upload

我正在尝试上传图片并将其链接到我的数据库,我使用了以下代码:

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>

我到达了最后一个阶段并且给出了错误,没有发送文件

我不知道我错过了哪里。

谢谢

2 个答案:

答案 0 :(得分:0)

将表单文件名更改为&#34; uploaded_file&#34;。您的脚本正在寻找一个名为&#34; file&#34;的不存在的文件帖子。而不是&#34; uploaded_file&#34;

答案 1 :(得分:0)

检查文件名: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}'
    )";