problem with accentuation when importing text file via php and writing to mysql

Asked

Viewed 1,029 times

0

I’m having a terrible fight to be able to record data obtained from a string-accentuated txt file.

It turns out that all accented words are not recorded in the table. See below the blank column, where you should have recorded this information: Compulsory retirement note a parte em destaque, está vazio.

The table in question is created in Innodb and UTF-8

When I open the file, I still use this: $lines= utf8_encode($lines)|

Well, what did I realize: If I open the text file on Notepad++ and check it for utf-8 NO GOOD, bingo, my problems are over.

But I can’t always ask my customers to open txt and convert before importing on the site... it is a daily routine and it generates certain discomfort such a procedure.

I thought to try to open the file and convert it to utf8 without BOM direct in PHP or treat the data at the time of import.

Someone can give me a light?

Here is my code:

        // A abertura do Arquivo
    $arquivo = fopen($_FILES['arquivo']['tmp_name'], 'r');
    //echo mb_detect_encoding($arquivo); exit;



        while(!feof($arquivo) ) {

            $linhas = fgets($arquivo);                 

            $linhas= utf8_encode($linhas);
            //echo mb_detect_encoding($linhas); exit;


            $dados = explode(";", $linhas);




            if ($linhas != "\n" && $linhas != ""):


            $conteudo = array();




            $conteudo[] = [
                'funcionario_cargo' => $dados[0],
                'funcionario_matricula' => $dados[1],
                'funcionario_nome' => Check::Name($dados[2]),                               
                'funcionario_title' => $dados[2],            
                'funcionario_cpf' => $dados[3],
                'funcionario_data_nascimento' => $dados[4],
                'funcionario_portaria' => $dados[5],
                'funcionario_data_beneficio' => $dados[6],
                'funcionario_data_beneficio_publicacao' => $dados[7],
                'funcionario_situacao' => $dados[8], 
                'funcionario_regra' => $dados[9],
                'funcionario_regramunicipal' => $dados[10],
                'funcionario_valor' => $dados[11],
                'funcionario_reajuste' => $dados[12],
                'funcionario_processo' => $dados[13],
                'funcionario_data_falecimento' => $dados[14],
                'funcionario_data_termino_pensao' => $dados[15],
                'funcionario_views' => 0,
                'funcionario_status' => 1,
                'funcionario_date' => date('Y-m-d H:i:s'),
                'funcionario_author' => 1,
            ];



            $Create->ExeCreateMulti(DB_FUNCIONARIO, $conteudo);
            //var_dump($Create);
            //$jSON['content'] =  "<b>Arquivo Importado com Sucesso!";


            endif;
        }




    fclose($arquivo);

2 answers

1


I don’t know how you are uploading these files. txt but already tried to put in php the header ?

header("Content-type: text/html;charset=utf-8");
  • I use it like this:;<meta charset="UTF-8"> I already have a function for the header: if (isset($_SESSION['userLogin']) && isset($_SESSION['userLogin']['user_level']) && $_SESSION['userLogin']['user_level'] >= 6):&#xA; header('Location: dashboard.php?wc=home');&#xA;endif; Can I create another header??? Or adapt to meta???

  • Swap this redirect header for a js or html script and test with the header I passed you echo "<script>location.href='dashboard.php?wc=home';</script>";

  • Show @Fernandojunior It worked, I changed the redirect and includes the header that I passed and worked. Thank you very much! After 10 days and many forums, I got the solution.

0

Usually I use this script to redirect in PHP

  /**
  * Função para redirecionar url's
  *
  * @param string $link - Url a ser redirecionada
  *
  */
  static function redireciona($link) {

    if ($link == -1){
      echo " <script>history.go(-1);</script>";
    }else{
      echo " <script>document.location.href='$link'</script>";
    }

  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.