我有以下 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/