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