php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #27730 fgetcsv() fails on 2 newlines
Submitted: 2004-03-27 08:50 UTC Modified: 2004-03-28 12:11 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: clemens at gutweiler dot net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.5 OS: Linux 2.6.3 (and 2.4.20)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: clemens at gutweiler dot net
New email:
PHP Version: OS:

 

 [2004-03-27 08:50 UTC] clemens at gutweiler dot net
Description:
------------
fgetcsv() breaks reading when a line has no cdata (eg. \n\n)

Reproduce code:
---------------
<?php
	
	$fn = tempnam( '/tmp', 'test' );
	if( $fp = fopen( $fn, 'w' ) ) {
		fwrite( $fp, "one;val1\ntwo;val2\n\nthree;val3\n" );
		fclose( $fp );
		
		if( $fp = fopen( $fn, 'r' ) ) {
			$items = array( );
			while( $d = fgetcsv( $fp, 4096, ';' ) ) {
				$items[] = $d;
			}
			fclose( $fp );
			var_dump( $items );
		}
	}
	
?>

Expected result:
----------------
$items-count of 4 

Actual result:
--------------
$item-count of 2

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-28 12:03 UTC] rasmus@php.net
This isn't actually a bug.  I have changed this to a documentation problem as the example on the fgetcsv() page is not very good.  The text description of the function is however correct.  You get a zero-element array for blank lines and FALSE on end of file.  So your code should have a loop like this:

while( ($d = fgetcsv( $fp, 4096, ';' )) !== FALSE )
 [2004-03-28 12:11 UTC] rasmus@php.net
Fixed in the docs in CVS
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 05:01:33 2025 UTC