php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63341 auto_detect_line_endings does not work with MAC files
Submitted: 2012-10-23 15:10 UTC Modified: 2015-01-11 10:38 UTC
Votes:7
Avg. Score:4.6 ± 0.7
Reproduced:6 of 6 (100.0%)
Same Version:2 (33.3%)
Same OS:2 (33.3%)
From: tony at marston-home dot demon dot co dot uk Assigned:
Status: Closed Package: Filesystem function related
PHP Version: 5.4.8 OS: Windows 7
Private report: No CVE-ID:
 [2012-10-23 15:10 UTC] tony at marston-home dot demon dot co dot uk
Description:
------------
auto_detect_line_endings does not work with a CSV file containing MAC line endings.

Test script:
---------------
File 'mac-linebreaks.csv' contains the following:

label1,label2,label3\r
1a,1b,1c\r
2a,2b,2c\r
3a,3b,3c\r
4a,4b,4c\r

Each line terminates with carriage-return only "\r".

$handle = @fopen('mac-linebreaks.csv', 'r');
ini_set('auto_detect_line_endings', true);
$first = fgets($handle);

The variable $first now contains the whole file.

Expected result:
----------------
The variable $first should contain everything up to the first carriage-return, i.e. "label1,label2,label3"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-10 21:28 UTC] danack@php.net
-Status: Open +Status: Feedback
 [2015-01-10 21:28 UTC] danack@php.net
Hi, I couldn't reproduce this issue exactly, instead I saw that the output if "4a,4b,4c" when auto_detect_line_endings is false when the file is opened, and then turned on during the file reading.

However.....I don't think it's surprising that PHP is getting confused here. It's just not valid to change an ini setting that affects how PHP behaves in the middle of an arbitrary bit of code and expect PHP to work flawlessly. PHP seems to work correctly so long as the set how files are handled before the file is opened.

If that isn't the case, please let us know, otherwise this is just something that is not supported.





<?php

$filename = 'mac-linebreaks.csv';
$data = "label1,label2,label3\r1a,1b,1c\r2a,2b,2c\r3a,3b,3c\r4a,4b,4c\r";

file_put_contents($filename, $data);

ini_set('auto_detect_line_endings', true);
$handle = @fopen($filename, 'r');
$first = fgets($handle);

echo $first;


// Output is: label1,label2,label3
// i.e. correct when ini setting is set before file opening.



<?php

$filename = 'mac-linebreaks.csv';
$data = "label1,label2,label3\r1a,1b,1c\r2a,2b,2c\r3a,3b,3c\r4a,4b,4c\r";

file_put_contents($filename, $data);

ini_set('auto_detect_line_endings', false);
$handle = @fopen($filename, 'r');
ini_set('auto_detect_line_endings', true);
$first = fgets($handle);

echo $first;


// Output is: 4a,4b,4c
// i.e. setting the setting after the file is opened is too late.
 [2015-01-11 10:38 UTC] tony at marston-home dot demon dot co dot uk
-Status: Feedback +Status: Closed
 [2015-01-11 10:38 UTC] tony at marston-home dot demon dot co dot uk
Yes, this works fine.

Perhaps the documentation should be amended to specify that the call to ini_set() needs to come before the fopen().
 
PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Sun May 24 09:03:03 2015 UTC