上传大型文件laravel 5

时间:2016-09-30 06:58:19

标签: php laravel-5

当我尝试在laravel 5中上传大文件时出现令牌不匹配错误。我在表单中使用了令牌字段。

TokenMismatchException in VerifyCsrfToken.php line 53:

我已经尝试过几种方法:

  1. 增加upload_max_filesize和post_max_size。
  2. 更改php.ini文件并重新启动apache服务。
  3. 禁用该路由的csrf中间件,但在这种情况下不提取该文件!
  4. 以下是我的观点:

    <form id="csvform22345" role="form" class="form-horizontal" enctype="multipart/form-data" method="POST" action="{{ URL::to('admin/QC/uploadtomaindbcsv') }}">
      {!! Form::token() !!}
      {{--<input type="hidden" name="_token" value="{{ csrf_token() }}">--}}
       <input type="hidden" name="pid" value="{{ $pid }}">
                                        <div class="row">
                                            <div class="col-sm-8">
                                                <label class="control-label">Select File</label>
                                                <input id="csvfile2" type="file" name="file2" required class="file-loading">
                                            </div>
                                            <div class="col-sm-4">
                                                <div class="alert alert-info small mar-top">Only .csv files allowed.</div>
                                            </div>
                                        </div>
                                    </form>
    

    控制器功能:

        public function upload_to_maindb()
    {
        try
        {
        ini_set('upload_max_filesize', '2G');
        ini_set('post_max_size', '4G');
        ini_set('max_execution_time', '5000000');
        ini_set('max_input_time', '5000000');
        ini_set('memory_limit', '200M');
        //echo "try block" ; die;
        $now = new DateTime();
    
        $timestamp = $now->format('Y-m-d H:i:s');
        $pid = Input::get('pid');
        $file = Input::file('file2');
            $project = Projects::select('project_type')->where('project_id',$pid)->get()->first();
            $pt = $project->project_type;
        if($file !='') {
            $pid = Input::get('pid');
            $fp = fopen($file, 'r');
            //echo "its here";
            //die;
            $datas = array();
            unset($datas);
    
            while (($row = fgetcsv($fp)) !== FALSE) {
                $contact['company_name'] = $row[0];
                $contact['website'] = $row[1];
                $contact['contact_name'] = $row[2];
                $contact['first_name'] = $row[3];
                $contact['last_name'] = $row[4];
                $contact['email'] = $row[5];
                $contact['title'] = $row[6];
                $contact['address'] = $row[7];
                $contact['city'] = $row[8];
                $contact['state'] = $row[9];
                $contact['zip_code'] = $row[10];
                $contact['country'] = $row[11];
                $contact['phone_number'] = $row[12];
                $contact['fax_number'] = $row[13];
                $contact['industry'] = $row[14];
                $contact['sub_industry'] = $row[15];
                $contact['revenue'] = $row[16];
                $contact['employees'] = $row[17];
                $contact['data_category'] = $pt;
                /*
                $contact['sic_code'] = $row[18];
                $contact['naics_code'] = $row[19];
                $contact['linkedin_source'] = $row[20];
                */
                $contact['last_updated_via_project_id'] = $pid;
                $datas[] = $contact;
            }
            foreach (array_chunk($datas, 200) as $data) {
                Master_datarecords::insertIgnore($data);
            }
        }
        unset($datas);
        //echo "done"; die;
            $msg= "successfully inserted";
        }
        catch(\Exception $e)
            {
                $msg = "some error occured, please check the file.";
            }
    
        $approved_projects= array();
        $pids= array();
        $i=0;
        $projstat = ProjectuserHelpers::GetProjectStatusId("approved");
        $projects_object=Projects::where('project_status',$projstat)->get();
        if(!($projects_object->isEmpty())) {
            $projects = $projects_object->toArray();
            foreach ($projects as $val) {
                $approved_projects[$i]['pid'] = $val['project_id'];
                $approved_projects[$i]['projectname'] = $val['project_name'];
                $approved_projects[$i]['record_count'] = Transitional_records::where('fk_project_id', $val['project_id'])->count();
                $i++;
            }
        }
        return View::make('admin/home_page/QC/QCtomaindb',compact('approved_projects','msg'));
    }
    

    我在网上找不到这些东西。还有其他解决方案吗?

2 个答案:

答案 0 :(得分:2)

我认为这是因为post_max_size限制而发生的。

我增加了50M并且还增加了上传最大文件大小。

现在它正在运作。

答案 1 :(得分:0)

你进入文件php.ini。 找到这个:

  

max_execution_time和max_input_time。

编辑

  

max_input_time = 1440,max_execution_time = 1440。

我尝试并成功了