0
Gelera, I’m hitting myself a little hard to figure out what’s going on with a code I put together.
I have a loop that downloads files .csv from a server specific to my server. There are 9 files.
After the downloads finish I need to delete the bases, perform the inserts again and delete the files, it seemed simple but some things are getting out of control.
At first the file "insert_delete.php" received the 3 loops of the 9 files, but it got lost and registered logs stating duplicate insertions and delete files. So I separated it into 3 files, it stopped happening.
An odd question is that when I directly run the file "insert_delete.php" everything happens inside the conforms, but when I run by script the base is going blank, as if the TRUNCATE happened after the foreach.
I think there’s something going on and I don’t see it.
Come on the mess I made:
Script
//Contagem dos arquivos se houver mais que 8 arquivos realiza a solicitação de inserção, se não continua contando
function cont(){
    setTimeout(function(){
        $.get("query/cont_arquivos.php", function( data ) {
            if(data < 9){
                $('.box_loading').show();
                $(".cont").html(
                    "Aguardando arquivos "+data
                );
                cont();
            }else{
                $.get("insert_delete.php", function(){});
                $.get("insert_delete1.php", function(){
                    $('.box_loading').hide();
                });
                $.get("insert_delete2.php", function(){});
                $(".cont").html(
                    "Inserindo nas bases... "
                );
            }
        });
    },1000);
}
PHP(Insert delete)
<?php
include "conn.php";
$files1 = glob('nomedosarquivos*.csv'); // obten alguns arquivos da pasta
$sql = "TRUNCATE TABLE nomedatabela"; //apaga a base
$conn->query($sql);
if(count($files1)>2){
  foreach($files1 as $file){ // loop dos 3 arquivos
    if(is_file($file))
    if (($base = fopen($file, "r")) !== FALSE) {
        $count = 0;
        $ok = 0;
        $erro = 0;
        while (($data = fgetcsv($base, 0, ";")) !== FALSE) {
            $data = array_map("utf8_encode", $data);
            $count++; //contar as linhas
            if ($count == 1) { continue; } //não inserir os titulos
            if ($data[0] == "") { break; } //sai do while se este campo estiver em branco
            //validações de campos
            $sql = "INSERT into nomedatabela (campos....) VALUES (dados..)";
            if($conn->query($sql)==TRUE){
                $ok++;
                //somente conta as linhas que foram inseridas
            }else{
                //conta os erros
                $erro++;
                //executa o log informando qual erro e em qual linha aconteceu
            }
        }
        fclose ($base);
        if(unlink($file)){
            //executa o log informando que o arquivo foi deletado
        }else{
            //executa o log informando um erro ao deletar o arquivo
        }
      }
    }
  }
 ?>
You are used asynchronous ajax ($.get) calls waiting for it to run on the server synchronously. Try to chain calls.
– Pagotti