|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #42357 Add optional parameter for custom line ending to fputcsv()
Submitted: 2007-08-21 06:13 UTC Modified: 2021-03-29 12:54 UTC
Avg. Score:4.2 ± 0.8
Reproduced:8 of 8 (100.0%)
Same Version:3 (37.5%)
Same OS:5 (62.5%)
From: php at phihag dot de Assigned: girgias (profile)
Status: Closed Package: Filesystem function related
PHP Version: 5.2.4RC2 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: php at phihag dot de
New email:
PHP Version: OS:


 [2007-08-21 06:13 UTC] php at phihag dot de
Currently, there is no way to set a custom line ending when writing CSV files with fputcsv. For instance, this is needed when writing CSV for various Microsoft products which require "\r\n" as line ending.
Therefore, an optional parameter $ending (as for stream_get_line), which defaults to auto-detection (current behaviour), would be nice.

To ensure the result is still a valid CSV file, either document using anything else than line endings (or tabs etc.) is not a good idea, or define/use constants for the common line endings.

Reproduce code:
$f = fopen("php://stdout", "w");
fputcsv($f, array("a", "b c"), ",", '"',  "\r\n");

Expected result:
As a php string:
"a,\"b c\"\r\n"

Actual result:
n/a, without the last parameter: "a,\"b c\"\n" (hardcoded line ending in file.c)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-21 06:20 UTC]
Does it work if you open the file in text mode ("wt") ? That *should* do the trick.
 [2007-08-22 16:25 UTC] php at phihag dot de
No, same behavior (tested on Linux).

*Windows* offers a text-mode translation flag ('t') ...

Small correction of the original bug report: \n is always used, have a look at ext/standard/file.c:~1991:

        smart_str_appendc(&csvline, '\n');
 [2011-04-08 21:39 UTC]
-Package: Feature/Change Request +Package: Filesystem function related
 [2016-10-25 17:57 UTC]
> Small correction of the original bug report: \n is always used,
> […]

Indeed[1], and this is actually a bug. RFC 4180[2] states:

> Each record is located on a separate line, delimited by a line
> break (CRLF).

[1] <>
[2] <>
 [2021-03-29 12:54 UTC]
-Summary: add optional parameter for line ending to fputcsv +Summary: Add optional parameter for custom line ending to fputcsv() -Status: Open +Status: Closed -Assigned To: +Assigned To: girgias
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 25 23:01:30 2024 UTC