Graham
Graham

Reputation: 8151

How to stop \n becoming \r\n in php

I have a php script that creates a text file:

<?php
$f = fopen("x.mrg","wt");
fwrite($f,"testing\n123\n");
fclose($f);
?>

When I look at the output file in a hex editor, the \n has been changed to \r\n (0D 0A)

Complete hex output:

74 65 73 74 69 6E 67 0D 0A 31 32 33 0D 0A    testing..123..

How do I stop it doing that? Is there an ini file setting for this?

I'm using php 5.6.7 on windows 8.1

I'm using HxD to view the hex

Upvotes: 1

Views: 60

Answers (1)

hek2mgl
hek2mgl

Reputation: 157990

Windows differentiates between text and binary files. You are currently open the file in text mode using t. You need to open the file using b for binary mode instead:

$f = fopen("x.mrg","wb");

Check the notes in the manual page of fwrite().

Upvotes: 3

Related Questions