Processing of input file in PHP

1

Good morning. I would like to know how to make my ...

<input type="file" name="ent_reg">

Be processed in PHP, or upload the file to my hosting and save the path in MySQL database.

  

The table in MySQL would be like this

     

Columns 4

     

Identification, full name, password, and profile image.

     

Where "profile image" saves the route where the file was uploaded to my hosting, so I can call it when said user logs in.

I want the name of the file to be changed, I am working to assign a profile image for the user, that is, an avatar. If the user who uploaded the file is called "John Kalibur" then in the directories of my server is this way

  

htdocs / users / data / john68 / archivo_john68.jpg

How can I make this possible? Thanks for the time invested in me.

    
asked by Máxima Alekz 18.05.2016 в 07:19
source

2 answers

2

In the HTML part, with that input inside a form, the file would be sent without problems. Do the validation in client or server, and it will suit each one, but in my example, I will do it in the server part.

<?php
$ruta_base   = "avatares/";
$archivo     = $ruta_base . basename( $_FILES["ent_reg"]["name"] );
$Ok          = 1;
$tipo_imagen = pathinfo( $archivo, PATHINFO_EXTENSION );

//comprueba que es una imagen
if ( isset( $_POST["submit"] ) ) {
    $check = getimagesize( $_FILES["ent_reg"]["tmp_name"] );
    if ( $check !== false ) {
        echo "Es una imagen - " . $check["mime"] . ".";
        $Ok = 1;
    } else {
        echo "No es una imagen.";
        $Ok = 0;
    }
}

//comprueba si existe
if ( file_exists( $archivo ) ) {
    echo "El archivo ya existe.";
    $Ok = 0;
}

//valida la extensión
if ( $tipo_imagen != "jpg" && $tipo_imagen != "png" && $tipo_imagen != "jpeg" && $tipo_imagen != "gif" ) {
    echo "Solo aceptamos extensiones JPG, JPEG, PNG & GIF.";
    $Ok = 0;
}

//Sube el archivo, si se ha recibido un archivo válido
if ( $Ok == 0 ) {
    echo "El archivo no ha sido subido, lo sentimos.";
} else {
    if ( move_uploaded_file( $_FILES["ent_reg"]["tmp_name"], $archivo ) ) {
        echo "El archivo " . basename( $_FILES["ent_reg"]["name"] ) . " ha sido subido.";
    } else {
        echo "Lo sentimos, ha habido un error subiendo el archivo.";
    }
}

And from here, you just have to make an insert in the database with the path that you have stored in the $ file variable.

    
answered by 18.05.2016 / 09:07
source
1

That is not true if you have it in your form but it is also important that in your form you have this attribute enctype="multipart / form-data" <form id="UserPictureForm" method="POST" enctype="multipart/form-data"> Otherwise, greetings did not work.

    
answered by 01.12.2017 в 04:14