Txt generation, does not work PHP_EOL function

Asked

Viewed 163 times

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.

  • 4

    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.

2 answers

2


If the goal is to send a line break to HTML use the tag <br>

<span>Quebra de linha<br>dentro dum<br>texto</span>

If your goal sends a break to the console use the sequence " r n" that works on multiple platforms.

console.log('Esse é um teste\r\nsobre quebras de linha\r\nindependentes de plataforma \r\nem seu código JS');


The problem arises from the definition of the constant PHP_EOL:

PHP_EOL ( string ) The correct 'End of line' symbol for this platform. Available since PHP 5.0.2

PHP_EOL is the 'End of the Line' symbol for the Operating System of Server. Which makes it unrecognized by the client. That is, this constant is to be used in server internal strings and does not have client code application.

Just out of curiosity here is the C code that generates the constant the constant PHP_EOL

//php.h:

#ifdef PHP_WIN32
#   include "tsrm_win32.h"
#   ifdef PHP_EXPORTS
#       define PHPAPI __declspec(dllexport)
#   else
#       define PHPAPI __declspec(dllimport)
#   endif
#   define PHP_DIR_SEPARATOR '\\'
#   define PHP_EOL "\r\n"
#else
#   if defined(__GNUC__) && __GNUC__ >= 4
#       define PHPAPI __attribute__ ((visibility("default")))
#   else
#       define PHPAPI
#   endif
#   define THREAD_LS
#   define PHP_DIR_SEPARATOR '/'
#   define PHP_EOL "\n"
#endif

Source: https://github.com/php/php-src/blob/master/main/php.h

A line break is a line break A line break is a line break line breaking except when it’s not. Surprisingly, there are three different types of line breaks in the world of modern computing and OS X uses two of the three.

One might think that the innocent line breaks, that blank docile that tells us when paragraphs begin and end, it would be a relatively simple piece of computer engineering. Unfortunately, there’s more to the line break than meets the eye.

There are three different types of line breaks, all originally unique to major operating systems: Windows / DOS, Macintosh and Unix. A document using Mac line breaks would look horrible on a Windows system, and a document using line breaks Windows on Unix would also not be interpreted correctly. The cause of that is how line breaking is actually created. Mac, by default, uses a single return car ( ), represented as r. Unix, on the other hand, uses a single line advance ( ) n,. Windows goes one step further and uses both, creating a combination ( ) r n.

To make things even more interesting, until the emergence of OS X, OS-specific line breaks remained on their own environment and did not work well with other people. Windows understood just his brothers, Unix laughed madly at anything else, and the Mac just smiled consciously. OS X, however, understands both the original Mac line break regarding Unix line break.

Dornfest and Hemenway: Mac Hacks. My Translation.

-2

uses at the end of the line:

"\n"

tested in my local environment and worked like this, do not forget to put between double quotes.

Browser other questions tagged

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