CSV imports; all values is one row
I'm 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开发者_如何学Go (($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, when I'm interrogating the variable $data
it appears the contents of the CSV file is written to one row, rather than multiple rows. As a result, I get an array of 228 column values.
Why is this? Is my PHP script not detecting a new line correctly? If so, is there an option to fix this behaviour?
set the auto_detect_line_endings ini setting to true:
ini_set('auto_detect_line_endings', true);
Most likely problem is the line endings in your source data. Could you make a test CSV file with several columns and rows to confirm or eliminate the data you're testing with?
See also: http://www.php.net/manual/en/filesystem.configuration.php#ini.auto-detect-line-endings
精彩评论