1 votes

PHP error "Warning: Undefined array key " in image upload with $_FILES

I am learning PHP and I copied the PHP code from a video by YouTube since it seemed simple to me, however, I get the error:

Warning: Undefined array key "file" in C:\xampp\htdocs\TiendaVegana\index.php on line 348 .

The code is as follows:

index.php

<form id="productoNuevoForm" action="nuevoProducto.php" method="POST" enctype="multipart/formdata">

 <input type="text" class="form-control" id="inputNombreProducto" aria-describedby="emailHelp" placeholder="Nombre de producto" name="nombreProducto">
  <br>
  <input type="text" class="form-control" id="inputPrecioProducto" aria-describedby="emailHelp" placeholder="Precio" name="precioProducto">
  <br>
  Seleccionar imagen... <br>
  <input type="file" name="file" /> 
  <br>
  <br>

  <button class="btn btn-primary" type="submit" value="btnAgregar" name="accionAgregar">Agregar</button>

  </form>
<?php

echo $_FILES['file']["name"];

?>

I would like to understand why I get the error when I want to upload a file. Thank you very much!

0voto

Anthony Melchor Points 3

Well, your mistake is simple, you can't echo a $_FILES['file']["name"]; Because it does not exist.

In case you want to upload an "x" img you should configure that in your file you call: newProduct.php

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X