M. Falex M. Falex - 2 months ago 15
HTML Question

PHP file upload error Undefined index

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

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

?>
Comments