-1

重複の可能性:
PHP でファイルのアップロードを実行できない

PHPでファイルアップロードスクリプトを書くことを学ぼうとしています。なぜこれが機能しないのかわかりません。ご覧ください


<?php
$name=$_FILES["file"]["name"];

if(isset($name)) {
    if(!empty($name)) {
        echo $name;
    }
    else {
        echo 'Please choose a file';
    }
}
?>

エラーメッセージが表示されますNotice: Undefined index: file in


html部分は


<form action="submissions.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file" id="file" /> 
<input type="submit" name="submit" value="Submit" /></form>

Windowsでワンプを使用しています。エラーの原因は何ですか?

4

3 に答える 3

2

PHPコードを実行する前に、フォームが送信されたかどうかを確認する必要があります。

<?php
if (isset($_POST["submit"]) && $_POST["submit"] === "Submit") {

    if (isset($_FILES["file"]["name"])) {
        $name = $_FILES["file"]["name"];

        if(!empty($name)) {
            echo $name;
        }
        else {
            echo 'Please choose a file';
        }
    }
}
?>
于 2012-10-01T16:23:55.153 に答える
1

手がかりはエラーメッセージにあります。インデックス 'file' は FILES 配列に存在しません。フォームを送信する前にこのコードを持っているので推測できますか?

于 2012-10-01T16:33:08.557 に答える
0

最初に存在するかどうかを確認し、

if(isset($_FILES['FormFieldNameForFile']) && $_FILES['FormFieldNameForFile']['size']>0){ # will be 0 if no file uploaded

次に、フィールド コンポーネントの使用を確認します。

$_FILES['userfile']['name']  # The original name of the file on the client machine. 
$_FILES['userfile']['type']  # The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted. 
$_FILES['userfile']['size']  # The size, in bytes, of the uploaded file. 
$_FILES['userfile']['tmp_name']  # The temporary filename of the file in which the uploaded file was stored on the server. 
$_FILES['userfile']['error']  # The error code associated with this file upload
于 2012-10-01T16:25:54.730 に答える