PHP文件上传错误未定义的索引

时间:2016-10-01 11:26:35

标签: php html

我有这个HTML代码:

<form id="uploadForm" action="" method="post"  enctype= "multipart/form-data">
<input type="file" name="fileToUpload" >
<input type="submit" name="submit" value="Submit"><br />

这是PHP代码:

$target_dir = "/";
    $file = $_FILES['fileToUpload']['name'];
    $target_file = $target_dir . $file;
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
        $check = getimagesize($_FILES['fileToUpload']['tmp_name']);
        if($check !== false) {
            echo "File is an image - " . $check['mime'] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }

我想要上传文件但收到此通知消息:

  

注意:未定义的索引:fileToUpload

1 个答案:

答案 0 :(得分:0)

<form id="uploadForm" action="" method="post"  enctype= "multipart/form-data">
<input type="file" name="fileToUpload" >
<input type="submit" name="submit" value="Submit"><br />
</form>
<?php
if(isset($_POST["submit"]))
{
    $target_dir = "/";
    $file = $_FILES['fileToUpload']['name'];
    $target_file = $target_dir . $file;
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
        $check = getimagesize($_FILES['fileToUpload']['tmp_name']);
        if($check !== false) {
            echo "File is an image - " . $check['mime'] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
}

?>