PHP CSV 上传检查和跳过标头行


PHP CSV upload checking and skipping headers line

 if ($_FILES[csv][size] > 0) { 
    //get the csv file 
    $file = $_FILES[csv][tmp_name]; 
    $handle = fopen($file,"r"); 
    //loop through the csv file and insert into database 
    do { 
        if ($data[0]) { 
            mysql_query("INSERT INTO PowerFlex (customerCode, postCode,Name, Address1,Address2) VALUES 
                ( 
 '".addslashes($data[0])."', 
 '".addslashes($data[1])."', 
 '".addslashes($data[2])."', 
 '".addslashes($data[3])."', 
 '".addslashes($data[4])."', 
 '".addslashes($data[5])."', 
 etc
                ) 
            "); 
        } 
    } while ($data = fgetcsv($handle,1000,",",'"')); 
    // 
}

此代码将我的 CSV 上传到数据库中,但不跳过 CSV 第一行中的标头(字段),也不会根据数据库表的标头检查标头。我该怎么做?

你可以尝试这样的东西:

if ($_FILES[csv][size] > 0) { 
    //get the csv file 
    $file = $_FILES[csv][tmp_name]; 
    $handle = fopen($file,"r"); 
    //loop through the csv file and insert into database 
    for ($lines = 0; $data = fgetcsv($handle,1000,",",'"'); $lines++) {
        if ($lines == 0) continue;
        if ($data[0]) { 
            mysql_query("INSERT INTO PowerFlex (customerCode, postCode,Name, Address1,Address2) VALUES 
                ( 
 '".addslashes($data[0])."', 
 '".addslashes($data[1])."', 
 '".addslashes($data[2])."', 
 '".addslashes($data[3])."', 
 '".addslashes($data[4])."', 
 '".addslashes($data[5])."', 
 etc
                ) 
            "); 
        } 
    } 
    // 
}
$data = fgets(...); // <= skip first line as header
$data = fgets(...); // <= read first data
//loop through the csv file and insert into database 
do { 
    if ($data[0]) { 
      ...
    } 
} while ($data = fgetcsv($handle,1000,",",'"')); 

您的代码无法区分第一行与其他行。它还依赖于 CSV 中按特定顺序排列的列。

在开始时执行单独的初始fgetcsv()并构建一个$csv_fields数组,然后使用此数组按名称将文件的字段与表的字段映射。有人担心 csv 中缺少列以及冗余列,但我会在下面的代码中选择忽略它们。我相信你会找到一种更合适的方法来处理这些事情。

if ($_FILES[csv][size] > 0) 
  { 
  // the table fields
  $tbl_fields = array('customerCode', 'postCode,Name', 'Address1', 'Address2');
  //get the csv file 
  $file = $_FILES[csv][tmp_name]; 
  $handle = fopen($file,"r"); 
  // get the first line into a fields map
  $csv_fields = fgetcsv($handle,1000,",",'"');
  // we will insert only common fields
  $tbl_fields = array_intersect($tbl_fields,$csv_fields);
  // if there's not at least one common field, don't go on
  if(count($tbl_fields)>0)
    {
    // we need the table's field names as keys (see below)
    $tbl_fields = array_flip($tbl_fields);
   // now let's go after the data
    while($data = fgetcsv($handle,1000,",",'"'))
      {
      $data=array_map('addslashes',$data); // apply addslashes() to all values 
      $data=array_combine($csv_fields,$data); // csv fields assoc (key=>value)
      $data=array_intersect_key($data,$tbl_fields); // discard redundant
      $tbl_fields_str=implode("`,`",array_keys($data));
      $tbl_vals_str=implode("','",array_values($data));
      $q="INSERT INTO `PowerFlex` (`$tbl_fields_str`) VALUES ('$tbl_vals_str')";
      mysql_query($q);
      }
    }
  else
    echo "There's no data I can use!";
  }
if ($_FILES[csv][size] > 0) { 
    //get the csv file 
    $file = $_FILES[csv][tmp_name]; 
    $handle = fopen($file,"r"); 
    $data = fgetcsv($handle,1000,",",'"') // this for skipping first line in your file
    $data = fgetcsv($handle,1000,",",'"') //
    //loop through the csv file and insert into database 
    do { 
        if ($data[0]) { 
            mysql_query("INSERT INTO PowerFlex (customerCode, postCode,Name, Address1,Address2) VALUES 
                ( 
 '".addslashes($data[0])."', 
 '".addslashes($data[1])."', 
 '".addslashes($data[2])."', 
 '".addslashes($data[3])."', 
 '".addslashes($data[4])."', 
 '".addslashes($data[5])."', 
 etc
                ) 
            "); 
        } 
    } while ($data = fgetcsv($handle,1000,",",'"')); 
    // 
}