1

当我上传文件时,它显示数组文件为空,但是上传在我的 php.ini 中。

请查看我的代码,让我知道错误在哪里。

我在网上到处寻找解决方案,但没有找到,我在 centos 5.4 内核版本 2.6.18-164.el5 上使用 PHP 版本 5.3.25。

谢谢并恭祝安康

哈迪

html页面。

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

<?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>';
?>
4

0 回答 0