#27573 [Bgs]: fopen mode "r" not working properly

From: Date: Fri, 12 Mar 2004 07:00:09 +0000
Subject: #27573 [Bgs]: fopen mode "r" not working properly
References: 1  Groups: php.doc 
Request: Send a blank email to phpdoc+get-969359495@lists.php.net to get a copy of this message
ID: 27573 User updated by: ib at wupperonline dot de Reported By: ib at wupperonline dot de Status: Bogus Bug Type: Documentation problem Operating System: Windows 2000 PHP Version: 4.3.4 New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2004-03-11 13:34:45] wez@php.net ah, already documented then :-) ------------------------------------------------------------------------ [2004-03-11 13:32:13] wez@php.net That is correct; fopen was changed to work in binary mode by default in order to be more portable. I thought this was mentioned in the NEWS file, but apparently not. The change was made in PHP 4.3.2. The undocumented 't' mode has been a feature of the MSVCRT since forever. This stuff needs documenting fairly prominently. ------------------------------------------------------------------------ [2004-03-11 13:27:57] pollita@php.net From http://www.php.net/fopen Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter. As of PHP 4.3.2, the default mode is set to binary for all platforms that distinguish between binary and text mode. If you are having problems with your scripts after upgrading, try using the 't' flag as a workaround until you have made your script more portable as mentioned above. ------------------------------------------------------------------------ [2004-03-11 12:31:46] 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 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=27573&edit=1

« previous php.doc (#969359495) next »