Undefined index when uploading an image to the server

0

Good morning, I'm running into this error and I'm not finding the key.

Notice: Undefined index: pac_foto in C: \ wamp64 \ www \ tests \ pags \ ver_foto.php on line 32

PHP.INI:

File Uploads activado
Max_execution_time = 300
Max_input_time = 240
Memory_limit = 128M
Post_max_size = 16M

destination PHP file

$pac_foto = isset($_REQUEST['pac_foto'])?$_REQUEST['pac_foto']:'';
$foto_directorio_subida = 'imgs/';
$foto_directorio_subida .= basename($_FILES['pac_foto']['name']);
$foto_tipo_archivo = $_FILES['pac_foto']['type'];
    if (move_uploaded_file($_FILES['pac_foto']['tmp_name'], $foto_directorio_subida)) {
        echo "Foto subida correctamente";
    } else {
        echo "No se pudo guardar la foto.";
    }

HTML form (in another document)

<form enctype="multipart/form-data" action="verFoto" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="30000000" />
    <input type="file" name="pac_foto" />
    <input type="submit" name="pac_boton_enviar" value="Guardar Datos" />
</form>

I've done var_dump of $ pac_foto and it takes out the name of the file I'm uploading. However, I do var_dump of $ _FILES and it gives me an empty array.

What can I be doing wrong?

Greetings and thanks

    
asked by Jose Gonzalez 07.08.2017 в 19:35
source

1 answer

0

Regarding your notice:

  

Notice: Undefined index: pac_foto in C: \ wamp64 \ www \ tests \ pags \ ver_foto.php on line 32

  • You should start this variable in NULL at the start of your code PHP $pac_foto = NULL , let's say before declaring said variable.

Regarding your code PHP , in itself, you could improve some areas and add verifications PHP to get more security, let's see a different example, to see if I can get better guidance.

A possible example:

HTML

<form action="verfoto.php" method="POST" enctype="multipart/form-data">        
    <input type="file" name="pac_foto" />
    <input type="submit" name="pac_boton_enviar" value="Guardar Datos" />
</form>

PHP (verfoto.php)

<?php
//Definido y no NULL nuestro formulario.
if (isset($_POST)) {
    //Comprobamos que no este vacio nuestro input file.
    if (!file_exists($_FILES['pac_foto']['tmp_name'])) {
        echo 'Por favor selecciona una imagen para continuar';
        //Rediriges si quieres al formulario.
    } else {
        //obtenemos datos imagen.
        $file = $_FILES["pac_foto"];
        $nombre_img = $file["name"];            
        $extencion_img = $file["type"];
        $ruta_temporal = $file["tmp_name"];
        $tamano_img = $file["size"];
        $dimensiones = getimagesize($ruta_temporal);
        $ancho = $dimensiones[0];
        $altura = $dimensiones[1];
        $carpeta = "/imgs/$nombre_img";

        //Inicio error imagen en true.
        $img_error = true;

        //Comprobaciones error (¡Esto es a gusto colores!).
        if ($extencion_img != 'image/jpeg' && $extencion_img != 'image/jpg' && $extencion_img != 'image/png' && $extencion_img != 'image/gif') {
            echo "<b>$nombre_img</b>, no es una imagen valido, un imagen con extensión valido podría ser entre (.jpg, .jpeg, .png o .gif).";                      
        } elseif($tamano_img > 300000) {
            echo"El tamaño de tu imagen <b>$nombre_img</b>, supera los 300kb permitidos."; 
        } elseif($ancho > 500 || $altura > 500) {
            echo "La anchura y la altura de <b>$nombre_img</b> supera la máxima permitida de 500px.";                   
        } elseif($ancho < 250 || $altura < 250) {
            echo "La anchura y la altura de <b>$nombre_img</b> es inferior que la mínima permitida de 250px.";              
        } else { //Imagen correcto.
            //Reseteo error en false.       
            $img_error = false;
        }

        //Verdadero imagen.
        if ($nombre_img && $img_error===false) {
            //Cargamos imagen al servidor.
            if(move_uploaded_file($_FILES["pac_foto"]["tmp_name"], $carpeta)) { 
                 echo "Foto subida correctamente";          
            } else { //Falso, imagen no cargo.          
                echo "No se pudo guardar la foto.";     
            }
        }
    }           
}
?>

If you want to implement AJAX and create forms dynamically, here is an example of SOes: insert image with ajax

    
answered by 07.08.2017 в 21:36