Bug #63341 [Opn->Fbk]: auto_detect_line_endings does not work with MAC files

From: Date: Sat, 10 Jan 2015 21:28:11 +0000
Subject: Bug #63341 [Opn->Fbk]: auto_detect_line_endings does not work with MAC files
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-189870@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63341&edit=1

 ID:                 63341
 Updated by:         danack@php.net
 Reported by:        tony at marston-home dot demon dot co dot uk
 Summary:            auto_detect_line_endings does not work with MAC
                     files
-Status:             Open
+Status:             Feedback
 Type:               Bug
 Package:            Filesystem function related
 Operating System:   Windows 7
 PHP Version:        5.4.8
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2012-10-23 15:10:48] 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"



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=63341&edit=1


Thread (4 messages)

« previous php.bugs (#189870) next »