Fatal error: Maximum execution time of 30 seconds

0

Good afternoon I make an interface in which you can import data from an Excel (.CSV) but I want to insert more than 10,000 data but I get this error Fatal error: Maximum execution time of 30 seconds exceeded in functions.php on line 6

<?php

 function insertar_datos($id_Control,$Fecha,$Hora){
 		global $conexion;
 	$sentencia = "insert into reloj (id_Control,Fecha,Hora) values ('$id_Control','$Fecha','$Hora')";
 	$ejecutar = mysqli_query($conexion,$sentencia);
 
 	return $ejecutar;
 }
?>

Annex the code of the form to upload it to the BD

<?php

if (isset($_POST["enviar"])) {//nos permite recepcionar una variable que si exista y que no sea null
	require_once("conexion_excel.php");
	require_once("functions.php");
	$archivo = $_FILES["archivo"]["name"];
	$archivo_copiado= $_FILES["archivo"]["tmp_name"];
	$archivo_guardado = "copia_".$archivo;

	//echo $archivo."esta en la ruta temporal: " .$archivo_copiado;

	if (copy($archivo_copiado ,$archivo_guardado )) {
		//echo "se copeo correctamente el archivo temporal a nuestra carpeta de trabajo <br/>";
	}else{
		//echo "hubo un error <br/>";
	}
    
    if (file_exists($archivo_guardado)) {
    	 
    	 $fp = fopen($archivo_guardado,"r");//abrir un archivo
         $rows = 0;
         while ($datos = fgetcsv($fp , 7000 , ";")) {
         	    $rows ++;
         	   // echo $datos[0] ." ".$datos[1] ." ".$datos[2]." ".$datos[3] ."<br/>";
         	if ($rows > 1) {
         		$resultado = insertar_datos($datos[0],$datos[1],$datos[2]);
         	if($resultado){
         		//echo "se inserto los datos correctamente<br/>";
         	}else{
         		//echo "no se inserto <br/>";
         	}
         	}
         }



    }else{
    	echo "No existe el archivo copiado <br/>";
    }

}


?>
    
asked by Carlos 07.03.2018 в 20:39
source

2 answers

0

There are 2 ways to do it but that depends on the type of server you have. If you have access to PHP.ini you can do it from there by modifying this line. max_execution_time=30 where 30 is the number of seconds you want to configure to continue running a task.

Or you can do it at the beginning of the function using one of these 2 commands.

ini_set('max_execution_time', '300');
set_time_limit(300);

Of course if you make changes in the PHP.ini you should always do it carefully and restart apache to make the changes.

    
answered by 07.03.2018 / 20:45
source
0

Abounding in the response from Raul A. , the right way, if the environment where You are running, it allows you, it would be to use set_time_limit . However, at the beginning of the process you do not know exactly how long it will take, so estimating 300, 600 or 900 seconds can eventually lead to an edge case.

It would seem more correct that in each iteration you were calling again%% of%.

while ($datos = fgetcsv($fp , 7000 , ";")) {
  set_time_limit(300);
  // código de inserción
}

With that, for each record you extend the expiration time to 30 seconds from that moment.

    
answered by 07.03.2018 в 21:03