Check if an image has been sent by form

0

I have a problem that I do not know how to solve. I would like to know how I can check if an image has been sent through a form.

I have a form with different fields, one of them type of image

<form action="control.php" method="POST" enctype="multipart/form-data">

<!--Campos de tipo texto -->

<div class="form-group">
   <label for="imagen">Imagen</label>
   <input type="file" class="form-control-file" id="imagen" name="imagen" accept="image/*">
</div>

<div class="form-group">
    <button type="submit" class="btn btn-primary" name="crearProducto">Crear</button>
</div>
</form>

In the control.php file I tried to check if an image was sent in the following ways

if(isset($_POST["imagen"])){
    echo "Imagen enviada";
}else{
    echo "imagen no enviada";
}

and

if(isset($_FILE["imagen"])){
    echo "Imagen enviada";
}else{
    echo "Imagen no enviada";
}

but in both cases the else is executed when I send the image, how can I do this check?

    
asked by Peter Allen 14.09.2018 в 19:46
source

2 answers

0

if (isset ($ _ FILES ["image"])) {
    echo "Image sent";
// you can also try
print_r ($ _ FILES ["image"]); // will show you the information about the file sent
}
else {
echo "Image not sent";
}

This is the correct way to receive a file from the form

    
answered by 17.09.2018 в 16:11
0

Finally I have solved it in the following way:

$nombre_imagen = $_FILES['imagen']['name']; if($nombre_imagen!=""){ echo "Imagen enviada"; }else{ echo "Imagen no enviada"; }

    
answered by 18.09.2018 в 23:08