php - 在 laravel 中上传时如何验证 csv 文件?

标签 php mysql laravel-5 maatwebsite-excel

我使用的是 Laravel 框架 5.2。我正在使用 Maatwebsite Excel 软件包,我已成功安装并成功导入 CSV 格式文件,但问题是:

假设我有一个表,列是:

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

现在,当我上传包含三列的 CSV 文件(user_id、customer_name、date) 时,它已成功上传。

当我上传带有附加列的 CSV 格式文件时 (user_id, customer_name, date, hacking_column, time) 我需要显示一条错误消息,例如“您的 CSV 文件包含一些不需要的列” ”

谁能帮帮我。这是我的功能

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!'
            );
    }
}

和 Blade 文件:-

<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>

最佳答案

在这里我找到了自己的解决方案。我只是打开文件并获取第一个标题行。这是我的片段:-

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!');

}

关于php - 在 laravel 中上传时如何验证 csv 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42547435/

相关文章:

php - 在我的 php 脚本中为 loop() 消除这一层

php - 如何解决 laravel eloquent 中与 sql_mode=only_full_group_by 不兼容的问题?

php - 我如何在 Laravel 中显示分组值

php - laravel 5.1 以多对多关系获取每个类别的 5 个新闻

javascript - 如何将变量和数据从PHP传递到JavaScript?

php - 复杂的 MySQL CONCAT 存在加密问题

php - 获取laravel中带有特定标签的所有数据

php - 合并 json 字符串的唯一键的值

php - 如何使用 PHP 从给定的数据库结构打印二叉树?

c++ - 找不到 mysql.h 文件