Omit the first record and save in B.D. from the second. 2.-Subo file .CSV but if the same file changed the name does not support it

0

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>
    
asked by karina reyes 18.05.2018 в 00:07
source

1 answer

0

The problem of skipping the first line is given by why fgetcsv() advances the pointer. That is, every time we call fgetcsv() over the open pointer by fopen this progresses.

To restart the pointer we can use fseek() just before the while , that is:

if (($fichero = fopen($fileName, "r")) !== FALSE) {
    // obtener columnas y avanzar puntero
    $nombres_campos = fgetcsv($fichero, 0, ",", "\"", "\"");
    $num_campos = count($nombres_campos);

    // reiniciar el puntero
    fseek($fichero, 0);

    // Lee los registros
    while (($datos = fgetcsv($fichero, 0, ",", "\"", "\"")) !== FALSE) {
        // body while
    }
} 

As for the name, the Script that you show is designed to work only with .csv files, if you want to allow some other type of extension you should add it to the $allowedType

array     
answered by 18.05.2018 / 13:20
source