使用php上传图片时出错

Aks*_*hay 2 html php

我正在尝试上传图像,如图所示w3schools但它总是显示错误

Undefined index: file 
Run Code Online (Sandbox Code Playgroud)

这是代码

HTML

<form action="upload.php" method="post"  enctype="multipart/form-data">
    <!-- Upload image -->
    <input type="file" name="file" id="file">
    <input type="submit" value="Upload Image" name="submit">
</form>
Run Code Online (Sandbox Code Playgroud)

PHP

<?php
    if(!isset($_POST["submit"])){
        die('Error');
    }
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["file"]["tmp_name"]);
        if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
        } else {
        echo "File is not an image.";
        $uploadOk = 0;
        }
    }
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    if ($_FILES["file"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !=   "jpeg"
    && $imageFileType != "gif" ) {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
    }
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    } else {
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["file"]["name"]). " has been     uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
?>
Run Code Online (Sandbox Code Playgroud)

AAB*_*AAB 6

如果您使用的是WAMP或XAMPP,则设置了文件上传限制.如果文件大小超过2mb,则上传将无效.请尝试上传大小小于2mb的图像并查看其是否有效.要更改文件上载限制,请打开php.ini文件并修改此值

的upload_max_filesize = 2M

将2M替换为您希望提供的限制,例如6M或8M

以下链接说明了如何更改文件上载限制 PHP更改最大上载文件大小