Windows vs Linux PHP PHP_EOL - php data different

148 views Asked by At

I migrated a script from windows to rhel 8 and I have the following code in my php script:

$Data = file_get_contents('https://example.com/api/data'); 
$Array = json_decode($Data, true);
$Output = '';
foreach ($Array as $key => $value) {
 if ($Output != ''){$Output .= PHP_EOL;}
 $Output .= $value["id"] . ":" . $value["status"];
}
file_put_contents($Response,$Output);

In windows, it puts a nice new line in the data. In hrel it is not.

I did a simple test:

$Output = '1';
$Output .= PHP_EOL;
$Output .= '2';
$Output .= PHP_EOL;
$Output .= '3';
file_put_contents($Response,$Output);

If I do a hexdump I get the following:

sh-4.4$ hexdump -c active.resp
0000000   1  \n   2  \n   3
0000005

and

sh-4.4$ hexdump active.resp
0000000 0a31 0a32 0033
0000005

The hexdump with the -c looks correct.

If I create the same file with vi, the hexdump shows an additional \n and in hex, 0a31 0a32 0a33

I added an extra PHP_EOL at the end of the $output and it worked and the hexdump shows exactly the same as the hexdump on the vi created file.

I am at a loss. Do I need to add a newline before doing file_put_contents on linux?

Can someone please explain this and the hexdump without the last php_eol?

1

There are 1 answers

0
user999684 On BEST ANSWER

I got to the bottom of this. Here is what I am pretty sure happened. In linux, lines end with a new line. I built this data by concatenation without a newline (or more accurately an EOL) at the end. This must not be an issue with windows. I added a newline at the end and it now handles it fine. When jbase reads a file like this, it reads it as raw data I think, right or wrong.