You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Yii2框架中导入CSV时如何跳过文件首行?

How to Skip CSV Header Row in Yii2 actionUpload

Hey there! To skip the header row (your first line user1,name,pass,info) when importing CSV data, you just need to exclude the first line from your processing loop. Here are two straightforward ways to adjust your code:

Method 1: Remove the Header Row Before Looping

This approach cleans up the array of CSV lines first, so your loop only handles actual data rows. Here's the modified actionUpload method:

public function actionUpload(){ 
    $model = new CsvForm(); 
    if($model->load(Yii::$app->request->post())){ 
        $file = UploadedFile::getInstance($model,'file'); 
        $filename = 'Data.'.$file->extension; 
        $upload = $file->saveAs('uploads/'.$filename); 
        if($upload){ 
            $csv_file = 'uploads/' . $filename; 
            $filecsv = file($csv_file); 
            
            // Skip header: remove the first element from the CSV lines array
            if (!empty($filecsv)) {
                array_shift($filecsv);
            }
            
            foreach($filecsv as $data){ 
                $modelnew = new Customer1(); 
                $hasil = explode(",",$data); 
                $nim = $hasil[0]; 
                $nama = $hasil[1]; 
                $jurusan = $hasil[2]; 
                $angkatan = $hasil[3]; 
                $modelnew->username = $nim; 
                $modelnew->firstname = $nama; 
                $modelnew->lastname = $jurusan; 
                $modelnew->password = $angkatan; 
                $modelnew->save(); 
            } 
            unlink('uploads/'.$filename); 
            return $this->redirect(['site/index']); 
        } 
    }else{ 
        return $this->render('upload',['model'=>$model]); 
    } 
}

Method 2: Skip the First Iteration in the Loop

If you want to keep the original array intact, you can check the index of each row and skip the first one:

public function actionUpload(){ 
    $model = new CsvForm(); 
    if($model->load(Yii::$app->request->post())){ 
        $file = UploadedFile::getInstance($model,'file'); 
        $filename = 'Data.'.$file->extension; 
        $upload = $file->saveAs('uploads/'.$filename); 
        if($upload){ 
            $csv_file = 'uploads/' . $filename; 
            $filecsv = file($csv_file); 
            
            foreach($filecsv as $index => $data){ 
                // Skip the header row (index 0)
                if ($index === 0) {
                    continue;
                }
                
                $modelnew = new Customer1(); 
                $hasil = explode(",",$data); 
                $nim = $hasil[0]; 
                $nama = $hasil[1]; 
                $jurusan = $hasil[2]; 
                $angkatan = $hasil[3]; 
                $modelnew->username = $nim; 
                $modelnew->firstname = $nama; 
                $modelnew->lastname = $jurusan; 
                $modelnew->password = $angkatan; 
                $modelnew->save(); 
            } 
            unlink('uploads/'.$filename); 
            return $this->redirect(['site/index']); 
        } 
    }else{ 
        return $this->render('upload',['model'=>$model]); 
    } 
}

Quick Notes

  • The !empty($filecsv) check in Method 1 prevents errors if someone uploads an empty CSV file.
  • Both methods work reliably, but Method 1 is cleaner if you don't need to use the header row for anything else (like validating column order later).

内容的提问来源于stack exchange,提问作者Janardhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:55:33