how to validate the csv file while uploading in laravel?

kunal picture kunal · Mar 2, 2017 · Viewed 8.1k times · Source

I am using laravel framework 5.2. I am using Maatwebsite Excel package i have succesfully installed and succesfully imported CSV format files but problem is:

Suppose i have one table and columns are:

Table_name:- employees_schedule 
   columns:- user_id, customer_name, date,

Now when i upload CSV file with three columns (user_id, customer_name, date) it is successfully upload.

When I upload CSV format file, with additional columns example (user_id, customer_name, date, hacking_column, time) I need to show an error message, something like "Your CSV files has some unwanted columns"

Can anyone help me. Here is my function

public function UploadSchedule (Request $request)
{
    if ($request->isMethod('post')) {

        $data = $request->all();

        //echo "<pre>"; print_r($data); die;

        Excel::load(Input::file('schedule'), function ($reader) {
            $reader->each(function ($sheet) {
                EmployeeSchedule::firstOrCreate($sheet->toArray());
            });
        });

        return redirect()
            ->back()
            ->with(
                'flash_message_success', 
                'Your Employee Schedule Uploaded successfully!'
            );
    }
}

And blade file:-

<form id="addForm" role="form" class="form-horizontal" method="post"
  action="{{ url('admin/upload-employee-schedule') }}" 
  enctype="multipart/form-data">

  <input type="hidden" name="_token" value="{{{ csrf_token() }}}"/>

  <div class="form-body">
    <div class="form-group">
        <label class="col-md-3 control-label">Upload Schedule:</label>
        <div class="col-md-5">
            <input type="file" id="csv" name="schedule">
        </div>
    </div>
  </div>

  <div class="form-actions right1 text-center">
    <button id="check" class="btn green" type="submit">Submit</button>
  </div>
</form>

Answer

kunal picture kunal · Mar 2, 2017

Here i found the solution of my own. I just open the file and get the first header row. here is my snippet:-

public function UploadSchedule(Request $request){
if($request->isMethod('post')){
    $data = $request->all();
    $file = Input::file('schedule');
    $handle = fopen($file,"r");
    $header = fgetcsv($handle, 0, ',');
    $countheader= count($header); 
    if($countheader<4  && in_array('user_id',$header) && in_array('customer_name',$header) && in_array('date',$header)){
        Excel::load($file ,function($reader){
            $reader->each(function($sheet){
                $sheet['date'] = date('Y-m-d',strtotime($sheet['date']));
                EmployeeSchedule::firstOrCreate($sheet->toArray());
            });
        });
    } else {
        return redirect()->back()->with('flash_message_error', 'Your CSV files having unmatched Columns to our database...Your columns must be in this sequence <strong> user_id,customer_name,date </strong> only');
    }
    return redirect()->back()->with('flash_message_success', 'Your Employee Schedule Uploaded successfully!');

}