Upload multiple images to the server with PHP

2

I have a fragment of code that makes the upload of files, in this case images to the server and then saves the record in the database [MySQL], the operation works but sometimes I find it slow when trying to upload 2 or 3 images, which seems strange to me that result, I share the code that I have to see if you can give me any suggestions, Thanks in advance.

if(isset($_FILES['image']['tmp_name'])){
            #Numero de archivos a cargar
                $num_files = count($_FILES['image']['tmp_name']);
                for($i=0; $i < $num_files;$i++)
                {
                    #Ver si hay archivos para subir
                    if(!is_uploaded_file($_FILES['image']['tmp_name'][$i]))
                    {
                        echo "No se encontraron archivos";
                    }
                    else
                    {
                        #Copiar imagenes a la carpeta correspondiente de acuerdo a la marca
                        if(@copy($_FILES['image']['tmp_name'][$i],"$archivo".$_FILES['image']['name'][$i])){
                            $path = "".$_FILES['image']['name'][$i];
                            $query3= $con->query("INSERT INTO archivoticket(FK_idTicket, archivoTicket) VALUES('$idTick', '$path')");                       
                        }
                        else
                        {
                            echo "No se pudo subir el archivo";
                        }
                    }
                }
            }
    
asked by Silvestre Silva 19.10.2017 в 16:21
source

1 answer

1

The upload speed depends on the network and size of the files. however, in your code you can avoid making a connection to the base for each file. if you form the sql and execute it at the end:

if(isset($_FILES['image']['tmp_name'])){
#Numero de archivos a cargar
    $num_files = count($_FILES['image']['tmp_name']);
    $query = "INSERT INTO archivoticket(FK_idTicket, archivoTicket)";
    $values = '';
    for($i=0; $i < $num_files;$i++)
    {
        #Ver si hay archivos para subir
        if(!is_uploaded_file($_FILES['image']['tmp_name'][$i]))
        {
            echo "No se encontraron archivos";
        }
        else
        {
            #Copiar imagenes a la carpeta correspondiente de acuerdo a la marca
            if(@copy($_FILES['image']['tmp_name'][$i],"$archivo".$_FILES['image']['name'][$i])){
                $path = "".$_FILES['image']['name'][$i];
                $values .= "('$idTick', '$path'),"; // guardamos los datos para luego ejecutar en una sola instruccion             
            }
            else
            {
                echo "No se pudo subir el archivo";
            }
        }
    }

    if (!empty($values)) {

      $query = $query . ' VALUES ' . substr($values, 0, -1); //Concatenamos y quitamos la ultima coma de los values
      $query3= $con->query($query);  //ejecutamos el query

   }
}

If the sql statement fails, no file will be saved in the database and you will have to physically delete it to avoid having garbage on the server. but currently you have the same problem.

I recommend you accept it.

    
answered by 20.10.2017 в 17:19