Import CSV; all values ​​are one line

I am having a problem using a CSV file uploaded via a PHP form. Here is the code:

$row = 1; if (($handle = fopen($_FILES['csv']['tmp_name'], "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br />\n"; } } fclose($handle); } 

The only problem is that when polling the $data variable, the contents of the CSV file are written in one line, and not in several lines. As a result, I get an array of 228 column values.

Why is this? My PHP script does not detect newline correctly? If so, is it possible to correct this behavior?

+6
php csv
source share
2 answers

Most likely, the problem is the line endings in your source data. Can you make a test CSV file with multiple columns and rows to confirm or eliminate the data you are testing?

See also: http://www.php.net/manual/en/filesystem.configuration.php#ini.auto-detect-line-endings

+6
source share

set auto_detect_line_endings to true:

 ini_set('auto_detect_line_endings', true); 
+12
source share

All Articles