我正在尝试使用fgetCsv但由于某种原因它只是读取第一行。这是代码:
$fieldseparator = ",";
$lineseparator = "\r";
if(!file_exists($csvFile)) {
echo "<div id='error'>Cannot find uploaded file. Please try again</div>";
exit;
}
$file = fopen($csvFile,"r");
if(!$file) {
echo "<div id='error'>Error loading CSV file</div>";
exit;
}
$size = filesize($csvFile);
if(!$size) {
echo "<div id='warning'>File is empty</div>";
exit;
}
$query = "";
$content = fgetcsv($file,$size,$lineseparator);
fclose($file);
foreach($content as $data) {
$values = explode($fieldseparator,$data);
$query[$i] = "('".implode("','",$values)."')";
}
这只输出一行。这是CSV文件:
TSE-P01,1,WO47653897,RM,EcoQuiet,1
TSE-P02,1,WO47653898,RM,EcoQuiet,1
TSE-P03,1,WO47653899,RM,EcoQuiet,1
TSE-P04,1,WO47653900,RM,EcoQuiet,1
TSE-P05,1,WO47653901,RM,EcoQuiet,1
TSE-P06,1,WO47653902,RM,EcoQuiet,1
TSE-P07,1,WO47653903,RM,EcoQuiet,1
TSE-P08,1,WO47653904,RM,EcoQuiet,1
为什么会发生这种情况的任何想法?
答案 0 :(得分:10)
答案 1 :(得分:6)
此示例是来自w3schools http://www.w3schools.com/php/func_filesystem_fgetcsv.asp
的“示例2”$file = fopen("contacts.csv","r");
while(!feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
使用while循环,代码遍历整个文件/所有行..
或者..如果你这样做......
print_r(fgetcsv($file));
print_r(fgetcsv($file));
print_r(fgetcsv($file));
它只打印前3行..