Error Undefined index when trying to upload image in php

1

Greetings! I'm trying to upload an image from any pc directory, I've never tried this because I'm very lost, I hope you can help me! Thanks in advance. I'm going to skip code because there are several files. I have the form in "addProduct.php", it has already added enctype="multipart / form-data", the main code is this:

    if(isset($_POST["iniciar"])){   
            $conexion = new conexion();
            $foto = $_POST["foto"]; <----Linea 21

            if($conexion->insertarProducto($foto))
            {
                header("Location: productos.php");  
            }
            else
            {
                echo "No se ingreso correctamente el producto.";
            }
        }

Inside the form in html is the one in the image:

    <input type="file" tabindex="4" accept="image/png, .jpeg, .jpg" class="form-control" id="foto" name = "foto" placeholder="Nombre del producto" maxlength="100" required>

Finally it is sent to conexion.class.php, the code that I have there is this:

    public function insertarProducto($foto)
{
    $nombre_img = $_FILES['foto']['name'];
    $directorio = $_SERVER['DOCUMENT_ROOT'].'/Practica/imagenes/';
    move_uploaded_file($_FILES['foto']['tmp_name'],$directorio.$nombre_img);
    $consulta = "Call InsertarProducto('".$foto."')";
    if($this->conn->query($consulta)===true)
    {
        return true;
    }
    else
        return false;
}

I think I do not have anything left! When sending the data I get this error: Notice: Undefined index: photo in C: \ Users \ xxxxxx \ Desktop \ xampp \ htdocs \ Practice \ addProducts.php on line 21

    
asked by yunque78 14.12.2017 в 23:18
source

1 answer

1

Well a long time ago I did something similar with this tutorial:

Try this code

   // allow valid image file formats
   if(in_array($imgExt, $valid_extensions)){   
    // Check file size '5MB'
    if($imgSize < 5000000)    {
     move_uploaded_file($tmp_dir,$upload_dir.$userpic);
    }
    else{
     $errMSG = "Sorry, your file is too large.";
    }
   }
   else{
    $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";  
   }
  }
  

and I recommend adding this line of code, I do not see it in your code:

    error_reporting( ~E_NOTICE ); // avoid notice

tutorial

    
answered by 14.12.2017 в 23:34