|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #46367 fputcsv does not add the correct newline character on Windows
Submitted: 2008-10-22 17:00 UTC Modified: 2020-03-30 16:58 UTC
Avg. Score:4.4 ± 0.8
Reproduced:20 of 20 (100.0%)
Same Version:6 (30.0%)
Same OS:14 (70.0%)
From: Assigned: cmb (profile)
Status: Duplicate Package: *General Issues
PHP Version: 5.2.6 OS: Windows XP
Private report: No CVE-ID: None
 [2008-10-22 17:00 UTC]
Per the documentation for the fputcsv() function, it adds a newline to the end of the csv string it returns. However, it is hardcoded to be '\n' ( default for unix newline ), while Windows uses \r\n. PHP should do this as well.

Below is a patch to fix this issue; it uses the constant PHP_EOL to get the correct newline to use on the current platform:

Index: php-src/ext/standard/file.c
RCS file: /repository/php-src/ext/standard/file.c,v
retrieving revision 1.530
diff -r1.530 file.c
< 	smart_str_appendc(&csvline, '\n');
> 	smart_str_appendc(&csvline, PHP_EOL);

Reproduce code:
$array1 = array("a","b","c");
$array2 = array("d","e","f");

echo fputcsv($array1).fputcsv($array2);

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-06 13:49 UTC]
Updated earlier patch:

Index: file.c
RCS file: /repository/php-src/ext/standard/file.c,v
retrieving revision 1.530
diff -u -r1.530 file.c
--- file.c	21 Oct 2008 22:06:48 -0000	1.530
+++ file.c	22 Oct 2008 21:21:42 -0000
@@ -2104,7 +2104,7 @@
-	smart_str_appendc(&csvline, '\n');
+	smart_str_appendl(&csvline, PHP_EOL, sizeof(PHP_EOL));
 	ret = php_stream_write(stream, csvline.c, csvline.len);

Also below is a test case for this bug. Should fail currently on Windows.

Bug #46367 - fputcsv does not add the correct newline character on Windows

$array1 = array("a","b","c");
$array2 = array("a","b","c");

$data_file = dirname(__FILE__) . '/dump.txt';
$fp = fopen($data_file);



$csvfile = file_get_contents($data_file);

var_dump(stripos($csvfile,PHP_EOL) !== FALSE);

echo "Done\n";

 [2009-07-17 11:41 UTC] chris dot tatedavies at inflightproductions dot com
How long does the voting last? I need to know if this is going to be fixed. Or if it has been already. I've looked through the changelog to no avail. Its been over 8 months since the original report.

Thanks, Chris
 [2010-03-10 19:50 UTC] m_rayman at bigfoot dot com
For such a simple bug, this should have been solved the same week it was brought to attention...  The correct and tested patch is even included!
 [2010-03-10 20:41 UTC]
-Package: Feature/Change Request +Package: *General Issues
 [2010-03-10 20:41 UTC]
Applying the "correct" patch means that the resulting file is system dependent and might break what current users expect. Probably this needs to be configured (additional parameter?)
 [2012-09-19 10:22 UTC] gtisza at gmail dot com
See (add optional parameter for line ending to fputcsv).
 [2020-03-30 16:58 UTC]
-Status: Open +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2020-03-30 16:58 UTC]
Closing as duplicate of bug #42357 (whether it is a bug or a
feature request will yet have to be determined).
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 28 15:01:30 2024 UTC