0

ファイルを mysql にアップロードすると、「エラー! ファイルが送信されませんでした!」というエラーが表示されます。

これが私の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

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

2 に答える 2

0

フォームを適切に送信するには、ファイル以外のフォーム フィールドが少なくとも 1 つ必要です。$_FILES 配列は、$_POST 配列にデータがないと設定されません。ファイル入力フィールドの上に非表示のフォーム フィールドを追加すると修正されます。

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

または、次のように送信ボタンの入力タグに name 属性を追加します。

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

動作していないデモでは、ファイルがアップロードされても $_FILES 配列が空であることがわかります。

于 2013-08-12T22:48:14.993 に答える
0

あなたがHTTP Upload Disabledやっているときに取得する場合

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

file_uploadsサーバー上の はデフォルトでオフになっています

php.ini/ へのアクセス権があり、ファイルを変更できる場合-

file_uploads = 1

あなたの.htaccessファイルで -

php_value  file_uploads  1

またはあなたのphpページの上部に

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

http://www.php.net/manual/en/ini.core.php#ini.file-uploadsを参照してください

于 2013-08-12T23:16:22.320 に答える