0

I have this HTML code:

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

This is the PHP code:

$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;
        }

And I want upload file but I get this notice message:

Notice: Undefined index: fileToUpload

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
M. Falex
  • 79
  • 2
  • 8

1 Answers1

1
<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;
        }
}

?>