<?php

global $w_hora;
global $cantidad;

$w_hora = date("Y-m-d H:i:s"); 
echo "\n\n Carga CUITs a procesar\n\n - Inicio del Proceso: " . $w_hora . "\n";  
$cantidad = 0;

$connection = new PDO('dblib:host=192.168.10.15;dbname=Fidelitas;charset=utf8','sa','KraDQlo');
$directory = opendir('/var/www/html/compartido/insur/in/');
while ($archivo = readdir($directory)) //obtenemos un archivo y luego otro sucesivamente
{
    if (is_dir($archivo))//verificamos si es o no un directorio
    {
        echo "\n\n - Directorio: " . $archivo; 
    }
    else
    {
        echo "\n\n - Archivo: " . $archivo;
        $dirarch  = '/var/www/html/compartido/insur/in/' . $archivo;
				$contents = file_get_contents($dirarch); 
				$contents = trim($contents);
				$cuits = explode("\n", $contents);
				foreach ($cuits as $key => $cuit) {
					$stmt  = $connection->prepare("EXEC procesamiento_insur_insertar '$cuit'");
					$stmt->setFetchMode(PDO::FETCH_OBJ); 
					if ($stmt) {
						$stmt->execute();
					}
					$cantidad += 1; 
				}
    }
}

echo "\n\n - Cuits cargados: " . $cantidad; 
$w_hora = date("Y-m-d H:i:s"); 
echo "\n\n - Fin del Proceso: " . $w_hora; 

?>