PHP读取cvs文件,省略逗号


PHP reading cvs file, when comma is left out

你好,我是PHP的新手,我正在尝试允许网页的用户上传一个csv文件,然后将行存储到MySQL行中。除了cvs文件在每一行的末尾都省略了逗号,导致下一行的最后一个单元格和第一个单元格合并之外,其他一切都很好。我哪里错了?谢谢

PHP文件,在提交表单时执行:

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$file_handle = fopen($target_file, "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
//print $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "<BR>";

$arrlength = count($line_of_text);
for($x = 0; $x < $arrlength; $x = $x + 1){
echo $x. "   ".$line_of_text[$x]. "<br>";   
}

for($x = 7; $x < $arrlength; $x = $x + 6) {


 $insertSQL = sprintf("INSERT INTO `tooneate_MyTrivia`.`Question` (
   `CategoryId`,
   `Question`,
   `Group`, 
   `AnsOne`, 
   `AnsTwo`, 
   `AnsThree`, 
   `AnsFour`, 
   `CorrectAns`) VALUES (%d, %s, %s, %s, %s, %s, %s, %d)",
                   GetSQLValueString($line_of_text[$x], "int"),//CatId
                   GetSQLValueString($line_of_text[$x+1], "text"),//Question
                   GetSQLValueString("1", "text"),//Group
                   GetSQLValueString($line_of_text[$x+2], "text"),//AnsOne
                   GetSQLValueString($line_of_text[$x+3], "text"),//AnsTwo
                   GetSQLValueString($line_of_text[$x+4], "text"),//AnsThree
                   GetSQLValueString($line_of_text[$x+5], "text"),//AnsFour
                   GetSQLValueString($line_of_text[$x+6], "int"));//CorrectAns
                   echo $insertSQL;
                    echo "<br>";
  mysql_select_db($database_TooNeate, $TooNeate);
  $Result1 = mysql_query($insertSQL, $TooNeate) or die(mysql_error());
  $insertGoTo = "MyTrivia.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));

}
}
fclose($file_handle);

以下是正在上传的csv文件示例:

类别Id,问题,答案一,答案二,答案三,答案四,正确答案

1、CSV文件工作吗?,是,不是,也许,嗯,1

这很酷吗?,是,不是,也许,呃,4

在上面的csv文件中,单元格"正确答案"与下一个"1"合并,"1"与"1"合并

谢谢!

这应该可以工作。

基本上,我将fopen()更改为file(),这样每个csv行都将是$csv_rows中自己的数组元素。file()函数对newline敏感,表示要处理丢失,的行结尾。

然后在循环中,我使用PHP str_getcsv()函数将行数据拆分为自己的数组。

<?php
$target_dir = '';
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]['tmp_name'],$target_file);
ini_set('auto_detect_line_endings', true); // Optional if you need it
$csv_rows = file($target_file);
mysql_select_db($database_TooNeate, $TooNeate);
foreach( $csv_rows as $_key => $_row ):
    if($_key==0) continue; // Skip header row
    $_row_field_data = str_getcsv($_row);
    $insertSQL = sprintf("INSERT INTO `tooneate_MyTrivia`.`Question` (
        `CategoryId`,
        `Question`,
        `Group`, 
        `AnsOne`, 
        `AnsTwo`, 
        `AnsThree`, 
        `AnsFour`, 
        `CorrectAns`) VALUES (%d, %s, %s, %s, %s, %s, %s, %d)",
        GetSQLValueString($_row_field_data[0], "int"),//CatId
        GetSQLValueString($_row_field_data[1], "text"),//Question
        GetSQLValueString("1", "text"),//Group
        GetSQLValueString($_row_field_data[2], "text"),//AnsOne
        GetSQLValueString($_row_field_data[3], "text"),//AnsTwo
        GetSQLValueString($_row_field_data[4], "text"),//AnsThree
        GetSQLValueString($_row_field_data[5], "text"),//AnsFour
        GetSQLValueString($_row_field_data[6], "int"));//CorrectAns
    echo $insertSQL;
    echo "<br/>";
    $Result1 = mysql_query($insertSQL, $TooNeate) or die(mysql_error());
endforeach;
$insertGoTo = "MyTrivia.php";
if (isset($_SERVER['QUERY_STRING'])):
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
endif;
//header(sprintf("Location: %s", $insertGoTo)); //  Uncomment this when you're ready for production.
?>

正如@CBroe在评论中提到的:

注意:如果PHP在读取时没有正确识别行尾Macintosh计算机上或由Macintosh计算机创建的文件,启用auto_detect_line_endings运行时配置选项可能会有所帮助解决问题。

如果您最终需要它,可以使用以下命令将其设置在PHP脚本的顶部:

ini_set('auto_detect_line_endings', true);