简体   繁体   English

将文件上传到php中的mysql数据库时出错

[英]error in uploading file into mysql database in php

I'm getting an error when i upload a file into mysql: "Error! A file was not sent!". 将文件上传到mysql时出现错误:“错误!未发送文件!”。

Here is my PHP code please look into it and let me know where the error is in my code. 这是我的PHP代码,请仔细研究一下,让我知道我的代码中的错误所在。

<!DOCTYPE html>
<head>
<title>MySQL file upload example</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="add_file.php" method="post" enctype="multipart/form-data">
    <input type="file" name="uploaded_file"><br>
    <input type="submit" value="Upload file">
</form>
<p>
    <a href="list_files.php">See all files</a>
</p>
</body>
</html>

add_file.php add_file.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('127.0.0.1', 'user', 'pwd', 'myTable');
        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 `file` (
        `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>';
?>

You need at least one non-file form field to have the form submit properly. 您至少需要一个非文件表单字段才能正确提交表单。 The $_FILES array does not get set without data in the $_POST array. 如果没有$ _POST数组中的数据,则不会设置$ _FILES数组。 Adding a hidden form field above your file input field will fix it. 在文件输入字段上方添加隐藏的表单字段将解决此问题。

<input type="hidden" name="form_field">
<input type="file" name="uploaded_file"><br>

Or add a name attribute to the submit button input tag like this: 或将名称属性添加到提交按钮输入标签,如下所示:

<input type="submit" name="submit" value="Upload file">

In the non working demo you'll see that, even though the file is uploaded the $_FILES array is empty. 在不工作的演示中,即使上传了文件,您也会看到$ _FILES数组为空。

If you get HTTP Upload Disabled when doing 如果执行此操作时HTTP Upload Disabled

if(ini_get('file_uploads') == 1){
   echo 'HTTP Upload Enabled';
} 
else { 
   echo 'HTTP Upload Disabled';
}

file_uploads on your server is turned off by default 默认情况下,服务器上的file_uploads是关闭的

If you have access to / and can modify your php.ini file - 如果您有权访问/,并且可以修改php.ini文件-

file_uploads = 1

In your .htaccess file - 在您的.htaccess文件中-

php_value  file_uploads  1

Or at the top your php page 或在您的php页面顶部

<?php
  ini_set('file_uploads',1);
?>

see http://www.php.net/manual/en/ini.core.php#ini.file-uploads 参见http://www.php.net/manual/zh/ini.core.php#ini.file-uploads

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

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