indefinite index $ _FILES

0

I have a problem with $ _FILES PHP to upload a photo Indefinite index fileToUpload this is my code

HTML

<form action="upload.php" method="post" enctype="multipart/form-data">
     Seleccionar imagen:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

PHP

//el error aparece en $_FILES["fileToUpload"]["name"]
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if(isset($_POST["submit"])) {

    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "El archivo es una imagen - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "El archivo no es una imagen.";
        $uploadOk = 0;
    }
}

I have tried in different ways but the result is always the same:

  

Notice: Undefined index: fileToUpload

When you run var_dump($_FILES) it returns me:

  

array (0) {}

but if I run it this way var_dump($_FILES['foto']['name']) keeps generating the same error.

    
asked by andres 24.04.2017 в 22:14
source

1 answer

0

You can solve it by inserting the lines of code that have to do with the image, inside the If that validates if the button name="submit" has been pressed. In addition to validating if the image was loaded on the server without errors, verifying that the error code other than 4.

$target_dir = "uploads/";

// print_r($_FILES);

if(isset($_POST["submit"])) {

    if($_FILES["fileToUpload"]["error"] != 4) {

        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

        $uploadOk = 1;

        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
            echo "El archivo es una imagen - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "El archivo no es una imagen.";
            $uploadOk = 0;
        }

    }

}

Greetings

    
answered by 24.04.2017 / 23:22
source