I have the following code in php which generated a file upload .CSV
the records should be temporarily saved in the B.D. and he does, but he saves them from the second record and the first always omits it.
Why? I can not find my mistake.
My second question is: they gave me a test file, called "example.csv" with which I have done all my tests but yesterday I changed the name to the file by: "Input1.csv" and it reads it but it does not save no record within the BD
//---------------------------------CODIGO PHP
<?php
// -- Limpio la tabla para nuevas inserciones
mysql_query('TRUNCATE tef.archivo;'); //Limpia registro tabla
if(isset($_POST['uploadBtn'])) {
$fileName = $_FILES['myFile']['name'];
$fileTmpName = $_FILES['myFile']['tmp_name'];
$fileExtension = pathinfo($fileName,PATHINFO_EXTENSION);
$allowedType = array('csv');
if(!in_array($fileExtension,$allowedType)){
?>
<div class="alert alert-danger">
ARCHIVO INVALIDO
</div>
<?php
}
else {
$registros = array();
if (($fichero = fopen($fileName, "r")) !== FALSE) {
$nombres_campos = fgetcsv($fichero, 0, ",", "\"", "\"");
$num_campos = count($nombres_campos);
// Lee los registros
while (($datos = fgetcsv($fichero, 0, ",", "\"", "\"")) !== FALSE) {
for ($icampo = 0; $icampo < $num_campos; $icampo++) {
$registro[$nombres_campos[$icampo]] = $datos[$icampo];
}
$registros[] = $registro;
}
fclose($fichero);
?>
<div class="alert alert-success">
<?php
echo "Archivo: \n". $fileName.$registros[$i][$nombres_campos[0]]. "\n Leidos " . count($registros[$i]) . " registros <br>";
?>
</div>
<?php
for ($i = 0; $i < count($registros); $i++) {
$query = "INSERT INTO tef.archivo (numero_proveedor,nombre_proveedor,monto_pago)
VALUES ('".$registros[$i][$nombres_campos[0]]."','".$registros[$i][$nombres_campos[1]]."','".$registros[$i][$nombres_campos[2]]."')";
$run = mysql_query($query);
}
}
}
}
?>
<div class="panel panel-primary">
<div class="panel-heading">
<h4>Seleccione un archivo </h4>
<!--FORM -->
<form action="" method="post" enctype="multipart/form-data">
</div>
<div class="panel-body">
<div class="col-md-6">
<div class="form-group">
<input type="file" name="myFile" class="form-control">
</div>
</div>
</div>
<div class="panel-footer">
<input type="submit" name ="uploadBtn" class="btn btn-info btn-md">
</div>
</form>
enter code here
</div>
</div>
</body>
<footer class="footer">
</footer>
</html>