我正在尝试上传文件,但是我的语法有一些错误

时间:2018-11-03 18:56:37

标签: laravel-5

我正在尝试使用https://www.youtube.com/watch?v=6P_nqOX38CE上传excel文件,但出现此错误...

  

array_combine():两个参数应具有相等数量的元素

我不知道该怎么做,因为我是laravel的新手。如果有人对我的laravel项目中创建excel上传功能有更好的建议,我也将不胜感激 这是我的控制器:

public function parseImport(Request $request)
{
    //get file
    $upload = $request->file('csv_file');
    $filePath = $upload->getRealPath();
    //open and read
    $file = fopen($filePath, 'r');
    $header = fgetcsv($file);
    // dd($header);
    $escapedHeader=[];
    //validate
    foreach ($header as $key => $value) {
        $lheader=strtolower($value);
        $escapedItem=preg_replace('/[^a-z]/', '', $lheader);
        array_push($escapedHeader, $escapedItem);
    }
    //looping through othe columns
    while($columns=fgetcsv($file))
    {
        if($columns[0]=="")
        {
            continue;
        }
        //trim data
        foreach ($columns as $key => &$value) {
            $value=preg_replace("/[^A-Za-z0-9?! ]/","",$value);
        }
        $data = array_combine($escapedHeader, $columns);

        // Table update
        $stud = $data['student_id'];
        $fname = $data['first_name'];
        $midname = $data['middle_name'];
        $surname = $data['surname'];
        $gender = $data['gender'];
        $class = $data['class'];
        $stream = $data['stream'];

        $budget->save();
    }
}

}

0 个答案:

没有答案