ID: 46367 User updated by: [EMAIL PROTECTED] Reported By: [EMAIL PROTECTED] Status: Open Bug Type: Feature/Change Request Operating System: Windows XP PHP Version: 5.2.6 New Comment:
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)); smart_str_0(&csvline); ret = php_stream_write(stream, csvline.c, csvline.len); Also below is a test case for this bug. Should fail currently on Windows. --TEST-- Bug #46367 - fputcsv does not add the correct newline character on Windows --FILE-- <?php $array1 = array("a","b","c"); $array2 = array("a","b","c"); $data_file = dirname(__FILE__) . '/dump.txt'; $fp = fopen($data_file); fputcsv($fp,$array1); fputcsv($fp,$array2); fclose($fp); $csvfile = file_get_contents($data_file); var_dump(stripos($csvfile,PHP_EOL) !== FALSE); echo "Done\n"; unlink($data_file); ?> --EXPECT-- bool(true) Done Previous Comments: ------------------------------------------------------------------------ [2008-10-22 17:00:52] [EMAIL PROTECTED] Description: ------------ 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 2107c2107 < 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: ---------------- "a","b","c" "d","e","f" Actual result: -------------- "a","b","c""d","e","f" ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=46367&edit=1