Search code examples
phpundefined-index

$_POST["myFile"] is a valid index but $_FILES["myFile"]["name"] isn't


I am making a sign up page in php. I am getting the following errors:

Notice: Undefined index: myFile in C:\xampp\htdocs\project1\form.php on line 63

Notice: Undefined index: myFile in C:\xampp\htdocs\project1\form.php on line 66

Warning: getimagesize(): Filename cannot be empty in C:\xampp\htdocs\project1\form.php on line 66

<form method="post" action="" enctype="multipart/form-data">
    <fieldset>
        <legend>Personal information</legend>
        Firstname:<br />
        <input type="text" name="Firstname">
        <span class="error">* <?php echo $FirstnameErr; ?></span>
        <br />
        Lastname:<br />
        <input type="text" name="Lastname">
        <span class="error">* <?php echo $LastnameErr; ?></span>
        <br />
        Gender<br />
        <input type="radio" name="Gender" value="male"> Male
        <input type="radio" name="Gender" value="female"> Female
        <span class="error">* <?php echo $GenderErr; ?></span>
        <br />
        Email:<br />
        <input type="text" name="Email">
        <span class="error">* <?php echo $EmailErr; ?></span>
        <br />
        Write about yourself:<br />
        <textarea name="Bio" rows="10" cols="60">
        </textarea>
        <br />
        Profile Picture:<br />
        <input type="file" name="myFile" id="myFile">
        <span class="error">* </span> 
        <?php echo $message; ?>
        <br /><br />
    </fieldset>
    Username:<br />
    <input type="text" name="Username">
    <span class="error">* <?php echo $UsernameErr; ?></span>
    <br />
    Password:<br />
    <input type="password" name="Password">
    <span class="error">* <?php echo $PasswordErr; ?></span>
    <br /><br />
    <input type="submit" name="submit" value="Submit">
</form>

My php is:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["Firstname"]))
        $FirstnameErr = "Firstname is required";
    else
        $Firstname = test_input($_POST["Firstname"]);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["Lastname"]))
        $LastnameErr = "Lastname is required";
    else
        $Lastname = test_input($_POST["Lastname"]);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["Gender"]))
        $GenderErr = "Gender is required";
    else
        $Gender = test_input($_POST["Gender"]);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["Email"]))
        $EmailErr = "Email is required";
    else {
        $Email = test_input($_POST["Email"]);
        if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
            $EmailErr = "Invalid email format";
            $Email = "";
        }   
    }
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $Bio = test_input($_POST["Bio"]);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["Username"]))
        $UsernameErr = "Username is required";
    else
        $Username = test_input($_POST["Username"]);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["Password"]))
        $PasswordErr = "Password is required";
    else
        $Password = test_input($_POST["Password"]);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST["myFile"])) { 
        $target_dir = "asset/uploads/user/" . time();
        $target_file = $target_dir . basename($_FILES["myFile"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        $check = getimagesize($_FILES["myFile"]["tmp_name"]);
        if($check !== false) {
            $uploadOk = 1;
        }
        else {
            $uploadOk = 0;
        }

        if (file_exists($target_file)) {
            $uploadOk = 0;
        }

        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
            $uploadOk = 0;
        }

        if ($uploadOk == 0) {
            $message = "Sorry, your file was not uploaded.<br />";
        }
        else {
            if (move_uploaded_file($_FILES["myFile"]["tmp_name"], $target_file)) {
                $result = true;
                $filePath = $target_file;
            }
            else {
                $result = false;
                $filePath = $target_file;
            }
        }
    }   
}
?>

Can someone help me out here?


Solution

  • You have a typo.

    enctype:"multipart/form-data"
    

    : should be =.

    Since you aren't setting the attribute correctly, the request isn't multipart and the file isn't included.