|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16521 file() - incorrect behavior (line endings ignored) on files with Mac EOLN (CR)
Submitted: 2002-04-09 23:13 UTC Modified: 2002-12-19 07:38 UTC
Avg. Score:4.6 ± 0.7
Reproduced:10 of 12 (83.3%)
Same Version:4 (40.0%)
Same OS:2 (20.0%)
From: dphelan at bcps dot org Assigned:
Status: Closed Package: Filesystem function related
PHP Version: 4.1.2 OS: FreeBSD 4.5-Stable
Private report: No CVE-ID: None
 [2002-04-09 23:13 UTC] dphelan at bcps dot org
In PHP 4.1.2 (and 4.2.0-dev) Calling file() on a 4-line 
text file with Macintosh line endings (CR) results in a 
1-element array:

file: (file.txt)



$array = file( 'file.txt. );
echo count( $array );

>> Returns '1' (Verified by print_r)

file() is ignoring the line endings, but the correct result 
happens for files with Windows (CRLF) and Unix (LF) line 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-14 13:46 UTC]
This seems critical as this is such a commonly used function.  (and Derick promised it would be fixed by 4.3.0 :)
 [2002-09-23 13:36 UTC]
Fixed in HEAD.
Please try a non-stable snapshot and set the new ini option
auto_detect_line_endings=on in your php.ini/apache.conf.
The fix will be in 4.3.

PS: You might have to wait a couple of hours for this
change to appear on

 [2002-12-18 20:13 UTC] dreamchimney at yahoo dot com
is there a work around for this? just some way to go 
through the array containing the file, line by line.

Im using PHP 4.2.2
 [2002-12-19 07:38 UTC]
If you must use PHP < 4.3, you can do something like this:

$fp = fopen($filename, "r");
$data = fread($fp, filesize($filename));
$lines = explode("\r", $data);

BEWARE: file() keeps the \r at the end of each line,
whereas the code snippet above does not.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Feb 21 16:01:28 2024 UTC