Error trying to save txt

2

Personal good morning,  I have this code block:

$delimitador    = ';';
$cerca          = '"';
$VarDataAux     = '';
// Abrir arquivo para leitura
$f = fopen($_FILES['arquivo']['tmp_name'], 'r');
if ($f) {

    // Ler cabecalho do arquivo
    $cabecalho = fgetcsv($f, 0, $delimitador, $cerca);

    // Enquanto nao terminar o arquivo
    while (!feof($f)) {

        // Ler uma linha do arquivo
        $linha = fgetcsv($f, 0, $delimitador, $cerca);
        if (!$linha) {
            continue;
        }

        // Montar registro com valores indexados pelo cabecalho
        $registro = array_combine($cabecalho, $linha);

        $VarLoja      =  $registro['loja'].PHP_EOL;
        $VarData      =  $registro['Dtemi'].PHP_EOL;
        $VarDoc       =  $registro['Doc'].PHP_EOL;
        $VarFonec     =  $registro['Forn'].PHP_EOL;
        $VarNFornec   =  $registro['Nforn'].PHP_EOL;
        $VarCodCont   =  $registro['Codcont'].PHP_EOL;
        $VarValorN    =  $registro['Vlnota'].PHP_EOL;
        $VarValorInss =  $registro['Inss'].PHP_EOL;

        $VarDocFinal =  str_pad($VarDoc,8, '0', STR_PAD_LEFT);

if($VarDataAux != $VarData){
  echo $VarCabecalho =  "M;".$VarLote.";CSV PRODUCAO;".$VarData.PHP_EOL;
  echo $VarLinha =  "*P;".$VarDoc.";".$VarCodCont.";180;".$VarValorInss.";160;".$VarDocFinal." ".$VarNFornec.";".$VarLoja.";;;".$VarData.";;;".$VarLote.PHP_EOL;
}else{
  echo $VarLinha =  "*P;".$VarDoc.";".$VarCodCont.";180;".$VarValorInss.";160;".$VarDocFinal." ".$VarNFornec.";".$VarLoja.";;;".$VarData.";;;".$VarLote.PHP_EOL;
     }

     $VarDataAux = $VarData;

     $name = 'TESTE.txt';
     $text = $VarCabecalho.$VarLinha;
     $file = fopen($name, 'a');
     fwrite($file, $text);
     fclose($file);

   }
  fclose($f);

}

It receives a csv file and handles some information and creates the layout. Until then it's all right, I'm going to go to the browser and it's fine, as it has to be:

M;4444;CSV PRODUCAO;02/06/2017 
*P;30318 ; ;180;25,81 ;160;030318 JOAO DA SILVA ;2 ;;;02/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;05/06/2017 
*P;30359 ; ;180;4,34 ;160;030359 JOAO DA SILVA ;2 ;;;05/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;06/06/2017 
*P;30364 ; ;180;15,95 ;160;030364 JOAO DA SILVA ;2 ;;;06/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;07/06/2017 
*P;30393 ; ;180;4,37 ;160;030393 JOAO DA SILVA ;2 ;;;07/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;12/06/2017 
*P;30480 ; ;180;19,53 ;160;030480 JOAO DA SILVA ;2 ;;;12/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;16/06/2017 
*P;30529 ; ;180;25,4 ;160;030529 JOAO DA SILVA ;2 ;;;16/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;19/06/2017 
*P;30562 ; ;180;6,75 ;160;030562 JOAO DA SILVA ;2 ;;;19/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;20/06/2017 
*P;30594 ; ;180;15,98 ;160;030594 JOAO DA SILVA ;2 ;;;20/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;22/06/2017 
*P;30620 ; ;180;11,04 ;160;030620 JOAO DA SILVA ;2 ;;;22/06/2017 ;;;4444 
*P;30624 ; ;180;4,94 ;160;030624 JOAO DA SILVA ;2 ;;;22/06/2017 ;;;4444 
*P;30626 ; ;180;14,03 ;160;030626 JOAO DA SILVA ;2 ;;;22/06/2017 ;;;4444 

As you can see, I'm exporting it to a txt in this part:

 $name = 'TESTE.txt';
 $text = $VarCabecalho.$VarLinha;
 $file = fopen($name, 'a');
 fwrite($file, $text);
 fclose($file);

Except that in my txt this is exporting wrong, it is exporting like this:

;180;4,34
;160;030359
 JOAO DA SILVA                 
;2
;;;05/06/2017
;;;4444
M;4444;CSV PRODUCAO;06/06/2017

*P;30364
;              
;180;15,95
;160;030364
 JOAO DA SILVA                 
;2
;;;06/06/2017
;;;4444
M;4444;CSV PRODUCAO;07/06/2017

*P;30393
;              
;180;4,37
;160;030393
 JOAO DA SILVA                 
;2
;;;07/06/2017
;;;4444
M;4444;CSV PRODUCAO;12/06/2017

*P;30480
;              
;180;19,53
;160;030480
 JOAO DA SILVA                 
;2
;;;12/06/2017
;;;4444
M;4444;CSV PRODUCAO;16/06/2017

*P;30529
;              
;180;25,4
;160;030529
 JOAO DA SILVA                 
;2
;;;16/06/2017
;;;4444

The first line is respecting ok, except that the other line is breaking us; I need it in the same way that it is in the browser in txt.

I get csv like this:

loja;Dtemi;Doc;Forn;Nforn;Codcont;Vlnota;Inss
2;02/06/2017;30318;  7688/0;JOAO DA SILVA                 ;              ;1.122,40;25,81
2;05/06/2017;30359;  7688/0;JOAO DA SILVA                 ;              ;188,7;4,34
2;06/06/2017;30364;  7688/0;JOAO DA SILVA                 ;              ;693,6;15,95
2;07/06/2017;30393;  7688/0;JOAO DA SILVA                 ;              ;190,4;4,37
2;12/06/2017;30480;  7688/0;JOAO DA SILVA                 ;              ;849,4;19,53
2;16/06/2017;30529;  7688/0;JOAO DA SILVA                 ;              ;1.104,60;25,4
2;19/06/2017;30562;  7688/0;JOAO DA SILVA                 ;              ;293,84;6,75
2;20/06/2017;30594;  7688/0;JOAO DA SILVA                 ;              ;694,8;15,98
2;22/06/2017;30620;  7688/0;JOAO DA SILVA                 ;              ;480;11,04
2;22/06/2017;30624;  7688/0;JOAO DA SILVA                 ;              ;214,8;4,94
2;22/06/2017;30626;  7688/0;JOAO DA SILVA                 ;              ;610;14,03

I need to deliver the txt like this:

M;4444;CSV PRODUCAO;16/06/2017 
*P;30529 ; ;180;25,4 ;160;030529 JOAO DA SILVA ;2 ;;;16/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;19/06/2017 
*P;30562 ; ;180;6,75 ;160;030562 JOAO DA SILVA ;2 ;;;19/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;20/06/2017 
*P;30594 ; ;180;15,98 ;160;030594 JOAO DA SILVA ;2 ;;;20/06/2017 ;;;4444 
M;4444;CSV PRODUCAO;22/06/2017 
*P;30620 ; ;180;11,04 ;160;030620 JOAO DA SILVA ;2 ;;;22/06/2017 ;;;4444 
*P;30624 ; ;180;4,94 ;160;030624 JOAO DA SILVA ;2 ;;;22/06/2017 ;;;4444 
*P;30626 ; ;180;14,03 ;160;030626 JOAO DA SILVA ;2 ;;;22/06/2017 ;;;4444 
    
asked by anonymous 30.06.2017 / 13:34

1 answer

2

My initial idea was to respond with a more structured code and better to understand, but you did not make things easy. Lots of information omitted and ever more and more confusing, so the changes you need to make to work are:

  • Remove the PHP_EOL as suggested in the comments;

    $VarLoja      =  $registro['loja'];
    $VarData      =  $registro['Dtemi'];
    $VarDoc       =  $registro['Doc'];
    $VarFonec     =  $registro['Forn'];
    $VarNFornec   =  $registro['Nforn'];
    $VarCodCont   =  $registro['Codcont'];
    $VarValorN    =  $registro['Vlnota'];
    $VarValorInss =  $registro['Inss'];
    
  • Correct the text that is written to the TXT file. Having $text = $VarCabecalho.$VarLinha will always cause the row header to be written before all lines and you do not want this. As you said in the comments, you did it based on someone else's logic and you probably did not understand what you did. Then put $text inside if and remove this wrong;

    if($VarDataAux != $VarData){
      echo $VarCabecalho =  "M;".$VarLote.";CSV PRODUCAO;".$VarData.PHP_EOL;
      echo $VarLinha =  "*P;".$VarDoc.";".$VarCodCont.";180;".$VarValorInss.";160;".$VarDocFinal." ".$VarNFornec.";".$VarLoja.";;;".$VarData.";;;".$VarLote.PHP_EOL;
      $text = $VarCabecalho.$VarLinha;
    }else{
      echo $VarLinha =  "*P;".$VarDoc.";".$VarCodCont.";180;".$VarValorInss.";160;".$VarDocFinal." ".$VarNFornec.";".$VarLoja.";;;".$VarData.";;;".$VarLote.PHP_EOL;
      $text = $VarLinha;
    }
    
  • With this, your TXT should be generated like this:

    M;;CSV PRODUCAO;02/06/2017
    *P;30318;              ;180;25,81;160;00030318 JOAO DA SILVA                 ;2;;;02/06/2017;;;
    M;;CSV PRODUCAO;05/06/2017
    *P;30359;              ;180;4,34;160;00030359 JOAO DA SILVA                 ;2;;;05/06/2017;;;
    M;;CSV PRODUCAO;06/06/2017
    *P;30364;              ;180;15,95;160;00030364 JOAO DA SILVA                 ;2;;;06/06/2017;;;
    M;;CSV PRODUCAO;07/06/2017
    *P;30393;              ;180;4,37;160;00030393 JOAO DA SILVA                 ;2;;;07/06/2017;;;
    M;;CSV PRODUCAO;12/06/2017
    *P;30480;              ;180;19,53;160;00030480 JOAO DA SILVA                 ;2;;;12/06/2017;;;
    M;;CSV PRODUCAO;16/06/2017
    *P;30529;              ;180;25,4;160;00030529 JOAO DA SILVA                 ;2;;;16/06/2017;;;
    M;;CSV PRODUCAO;19/06/2017
    *P;30562;              ;180;6,75;160;00030562 JOAO DA SILVA                 ;2;;;19/06/2017;;;
    M;;CSV PRODUCAO;20/06/2017
    *P;30594;              ;180;15,98;160;00030594 JOAO DA SILVA                 ;2;;;20/06/2017;;;
    M;;CSV PRODUCAO;22/06/2017
    *P;30620;              ;180;11,04;160;00030620 JOAO DA SILVA                 ;2;;;22/06/2017;;;
    *P;30624;              ;180;4,94;160;00030624 JOAO DA SILVA                 ;2;;;22/06/2017;;;
    *P;30626;              ;180;14,03;160;00030626 JOAO DA SILVA                 ;2;;;22/06/2017;;;
    
      

    I set $VarLote = "" to not generate alerts, since this variable was completely omitted from the question. Therefore the result does not have such a value.

    If this is not the desired output, you'd better delete the question and create it again, but in an understandable and complete way, with all the information, including a minimal, complete, and verifiable example.

        
    30.06.2017 / 14:59