0
Good morning, everyone. I am working on a php page for generating a.txt export data file to be imported back into another system. I had programmed taking into account the server running php 7, but it was actually put into a php 5 server. The problem in this case is that the PHP_EOL function that should have a break at the end of each line is not having function. That is the code:
//gera o arquivo
$DadosProposta = '1|'.$_SESSION['Parametros']['cdEntidade'].'|'.$_SESSION['Parametros']['dtAnoProcesso'].'|'.$_SESSION['Parametros']['nrPregao'].'|'.str_replace('-', '', str_replace('/', '', str_replace('.', '', $_POST['nrDocumentoLicitante']))).'|'.strtoupper($_POST['nmLicitante']).'|'.$_POST['flMPE'].'|'.$_POST['tpAmbito'].'|'.str_replace('-', '', str_replace('.', '', $_POST['nrDocumentoRepresentante'])).'|'.strtoupper($_POST['nmRepresentante']).PHP_EOL;
$Propostas = $_POST['Proposta'];
foreach ($Propostas as $Proposta ) {
if($Proposta['vlProposta']!=''){
$DadosProposta.='2|'.$_SESSION['Parametros']['cdEntidade'].'|'.$_SESSION['Parametros']['dtAnoProcesso'].'|'.$_SESSION['Parametros']['nrPregao'].'|'.$Proposta['nrLote'].'|'.$Proposta['nrItem'].'|'.str_replace('-', '', str_replace('/', '', str_replace('.', '', $_POST['nrDocumentoLicitante']))).'|'.number_format($Proposta['vlProposta'],$_SESSION['Parametros']['nrCasasDecimais']).'|'.strtoupper($Proposta['dsMarca']).PHP_EOL;
};
};
$DadosProposta = trim($DadosProposta,'\r\n');
$nmArquivo = 'files/PropostaDigital'.$_SESSION['Parametros']['nrPregao'].'_'.$_SESSION['Parametros']['dtAnoProcesso'].'_'.str_replace('-', '', str_replace('/', '', str_replace('.', '', $_POST['nrDocumentoLicitante']))).'.txt';
$PropostaDigital = fopen($nmArquivo,'w');
fwrite($PropostaDigital,$DadosProposta);
fclose($PropostaDigital);
//força o donwload no cliente
header("Content-Type: application/txt");
header("Content-Length: ". filesize($nmArquivo));
header("Content-Disposition: attachment; filename=".basename($nmArquivo));
readfile($nmArquivo);
//exclui o arquivo do servidor
unlink($nmArquivo);
In practice the result should come out like this, for example:
1|12310|2020|29|22222222215487|M A FRIGHETTO EMPREENDIMENTOS|1|1|11146578987|MATHEUS AUGUSTO FRIGHETTO
2|12310|2020|29|1|1|22222222215487|12.00|
2|12310|2020|29|30|1|22222222215487|20.00|MMAR
2|12310|2020|29|31|1|22222222215487|300.00|LOL
However, the file is being generated like this:
1|12310|2020|29|22222222215487|M A FRIGHETTO EMPREENDIMENTOS|1|1|11146578987|MATHEUS AUGUSTO FRIGHETTO2|12310|2020|29|1|1|22222222215487|12.00|2|12310|2020|29|30|1|22222222215487|20.00|MMAR|2|12310|2020|29|31|1|22222222215487|300.00|LOL
Besides, for some even stranger reason, an html character is being included at the beginning of the file, and even after the file is closed it continues to write html code inside that file. When I did it on my personal computer I was generating everything right, but when I was put on the company server this is happening and I don’t know what else to do.
PHP_EOL is a constant, not a function. This constant represents the standard line break of the operating system, usually does not serve the described purpose (except by coincidence, which is usually not desirable). Need to check the break needed for the system that will use the data, not the OS, and explicitly define in the code.
– Bacco