|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-03-11 12:31 UTC] ib at wupperonline dot de
Description:
------------
Using mode "r" to fopen files under Windows doesn't open them in text mode (as it should), but in binary mode, thus working exactly like "rb". Reading these files, you'll get "\r\n" instead of "\n" for new line character.
Surprisingly, fopen accepts mode "rt" which isn't documented but works as "r" should. It opens in text mode and returns "\n" instead of "\r\n".
Reproduce code:
---------------
Create a file named dat.txt containing 4 bytes: "1", CR, LF, "2", LF.
$f = fopen("dat.txt", "r");
$d = fread($f, 1);
echo ord($d) . " ";
$d = fread($f, 1);
echo ord($d) . " ";
$d = fread($f, 1);
echo ord($d) . " ";
$d = fread($f, 1);
echo ord($d) . " ";
fclose($f);
Expected result:
----------------
49 10 50 10
Actual result:
--------------
49 13 10 50
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 15:00:01 2025 UTC |
Why is fopen mode "r" working in binary mode more portable? It would be more portable if I could use the same mode ("r") on all platforms - as text mode (which is, for example, what the gcc port for Windows does as well). There is already a special mode ("rb") for systems distinguishing between the modes (which is well documented, even for POSIX fopen, if I remember well). No need for another ("rt") one.