CSV 导入;所有值都是一行
我在使用通过 PHP 表单上传的 CSV 文件时遇到问题。代码如下:
$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);
}
唯一的问题是,当我询问变量 $data
时,CSV 文件的内容似乎被写入一行,而不是多行。结果,我得到了一个包含 228 个列值的数组。
这是为什么呢?我的 PHP 脚本是否无法正确检测新行?如果是这样,是否有解决此行为的选项?
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 (($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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置 auto_detect_line_endings ini 设置为真:
set the auto_detect_line_endings ini setting to true:
最可能的问题是源数据中的行结尾。您能否制作一个包含多个列和行的测试 CSV 文件来确认或消除您正在测试的数据?
另请参阅:http://www. php.net/manual/en/filesystem.configuration.php#ini.auto-detect-line-endings
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