Bug #63341 [PATCH]: auto_detect_line_endings does not work with MAC files

From: Date: Wed, 03 Nov 2021 20:48:55 +0000
Subject: Bug #63341 [PATCH]: auto_detect_line_endings does not work with MAC files
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237532@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
 Patch added by:     1211il.comvv@gmail.com
 Reported by:        tony at marston-home dot demon dot co dot uk
 Summary:            auto_detect_line_endings does not work with MAC
                     files
 Status:             Closed
 Type:               Bug
 Package:            Filesystem function related
 Operating System:   Windows 7
 PHP Version:        5.4.8
 Block user comment: N
 Private report:     N

 New Comment:

The following pull request has been associated:

Patch Name: Language evolution overview proposal
On GitHub:  https://github.com/php/php-rfcs/pull/2
Patch:      https://github.com/php/php-rfcs/pull/2.patch


Previous Comments:
------------------------------------------------------------------------
[2015-01-11 10:38:36] 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().

------------------------------------------------------------------------
[2015-01-10 21:28:10] 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.

------------------------------------------------------------------------
[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 (#237532) next »