|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [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"
PatchesPull Requests
Pull requests: 
 HistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 19:00:02 2025 UTC | 
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.