php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26427 fopen text-mode does not play nicely with heredoc
Submitted: 2003-11-26 11:06 UTC Modified: 2003-11-27 08:36 UTC
From: phpbug at lapage dot com Assigned: wez (profile)
Status: Not a bug Package: Filesystem function related
PHP Version: 4.3.2 OS: Win98
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phpbug at lapage dot com
New email:
PHP Version: OS:

 

 [2003-11-26 11:06 UTC] phpbug at lapage dot com
Description:
------------
Writing in text-mode does not play nicely with heredoc, in Win32.  Line endings become \r\r\n in the typical situation: php source code with \r\n endings.

This will break, for example, batch files written for Win98.

Reproduce code:
---------------
$fp= fopen('file.txt', 'wt');
fwrite($fp, <<<ENDTEXT
One line of text
Another line of text
Last line of text\n
ENDTEXT
);
fclose($fp);


Expected result:
----------------
One line of text\r\n
Another line of text\r\n
Last line of text\r\n


Actual result:
--------------
One line of text\r\r\n
Another line of text\r\r\n
Last line of text\r\n

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-27 01:01 UTC] sniper@php.net
Assigned to the father of this mode..

 [2003-11-27 07:41 UTC] wez@php.net
I didn't invent DOS, so I'm not the father of this mode ;-)

When you activate text-mode translation, the MS libc will convert \n into \r\n as you write to the file.

If you save your PHP script using DOS line endings, each line of a heredoc will end with \r\n characters (as that is what appears in the file).  If you write that to a file opened in text-mode, \r\n will be translated into \r\r\n as you have observed.

If you save your PHP script using UNIX line endings, then
each line of the heredoc will end with a single \n, and will be written to the file using \r\n.

So, what is the solution?
Be explicit; open the file in binary mode (the default now in 4.3.x), and write the required line endings yourself using \r\n explicitly (or implicitly by using a heredoc and a PHP script saved using DOS line endings).

Since this is expected behaviour and not really a bug, I'm marking this report as Bogus.
 [2003-11-27 08:36 UTC] phpbug at lapage dot com
The point of text-mode is to avoid that sort of workaround, and to make the code portable.

Here are some more arguments:

1. It's easy to fix in the php code.  Before translating \n to \r\n, delete all \r.

2. It's a problem that's hard to notice at first for the general user.  You wouldn't see the extra \r until it bit you (extra lines in ftp upload; batch file won't work, etc.).

3. Binary mode may be default, but text-mode should be text-mode.

4. Saving a php file with unix line endings while working in dos is not convenient.

Sorry to argue the bogosity, but this is an easy fix.  If anyone is using solo \r's in dos, they shouldn't be using text-mode.
 [2013-05-07 07:14 UTC] salathe@php.net
Automatic comment from SVN on behalf of salathe
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=330181
Log: Corrected example listing per comment by rks at rks dot org

--
Provided by anonymous #26427 ()
 [2013-05-07 07:17 UTC] salathe@php.net
Automatic comment from SVN on behalf of salathe
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=330182
Log: More proofreading still needs to be done on this page. I only looked at the first couple of paragraphs.

--
Provided by anonymous #26427 ()
 [2013-08-20 15:41 UTC] daverandom@php.net
Automatic comment from SVN on behalf of daverandom
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=331177
Log: The intended meaning is that &quot;optional&quot; modifies &quot;first parameter&quot;; what was written made it look like &quot;first&quot; modified &quot;optional parameter&quot;.

--
Provided by anonymous #26427 ()
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 17:01:29 2024 UTC