We help IT Professionals succeed at work.

getscsv won't detect new lines in CSV

SheppardDigital
on
Hi there,

I've attached a bit of code that I'm using the convert a CSV file to an array. The problem is, I've generated a CSV file on a Mac and the script won't detect new lines in the file.

I've tried setting
ini_set('auto_detect_line_endings',true);
but it didn't make any difference.

Does anyone have any ideas?


$row = 0;
			if (($handle = fopen($filename, "r")) !== FALSE) {
	    		while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
					$rows[] = $data;
	       	 		}
	    		fclose($handle);
				}

Open in new window

Comment
Watch Question

Two things:

1. Make sure you do the ini_set() before the fopen or it won't work.

2. Here is another solution posted on the php.net site:

 
<?php ini_set('auto_detect_line_endings', true); ?> 

--didn't work for me at first.  I had to also add: 

<?php stream_get_line($handle, 4096, "\r"); ?> 

With both statements it works like a dream.

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.