php fgetcsv 返回所有行

标签 php fgetcsv

我有以下 csv 文件:

upc/ean/isbn,item name,category,supplier id,cost price,unit price,tax 1 name,tax 1 percent,tax 2 name,tax 2 percent,quantity,reorder level,description,allow alt. description,item has serial number
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,

当我这样做时:

if (($handle = fopen($_FILES['file_path']['tmp_name'], "r")) !== FALSE) 
{
    $data = fgetcsv($handle);
    var_dump($data);
}

我得到一个包含 183 个元素的数组,我希望只得到 csv 的一行,但我得到了整个文件。

我什至尝试了 $data = fgetcsv($handle, 1000); 我得到了相同的结果

最佳答案

这很可能是行尾问题。尝试启用 auto_detect_line_endings这将尝试确定文件的行尾。

ini_set('auto_detect_line_endings', true);

如果这不能解决问题,则使用 file检测行终止符的类型命令:

$ file example.csv
example.csv: ASCII text, with CR line terminators

然后您可以转换行尾。我不确定您使用的是什么操作系统,但有很多 utilities out there for file format conversion ,例如dos2unix .

关于php fgetcsv 返回所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5539053/

相关文章:

php - str_getcsv 未正确解析数据

php - 从外部 FTP 服务器读取 > 1GB GZipped CSV 文件

php - Godaddy 更新了 Apache,现在 fgetcsv 插入 mysql 脚本停止工作

php - fgetcsv 无法读取以 mac 格式的 csv 文件结尾的行,有更好的解决方案吗?

php - 从 CSV 文件中删除行

javascript - 使用提交按钮将 html 数据检索到表单输入中

javascript - MovieMasher 实用脚本

php - 在两个日期之间过滤 MYSQL

php - 我无法在 PHP 中指定自定义错误日志

php - 设置 cron 作业为绝对路径