php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #27573 fopen mode "r" not working properly
Submitted: 2004-03-11 12:31 UTC Modified: 2004-03-12 02:00 UTC
From: ib at wupperonline dot de Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 4.3.4 OS: Windows 2000
Private report: No CVE-ID: None
 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-11 13:27 UTC] 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 13:32 UTC] 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:34 UTC] wez@php.net
ah, already documented then :-)
 [2004-03-12 02:00 UTC] ib at wupperonline dot de
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 23:01:29 2024 UTC