Laravel 5.2 TokenMismatchException

时间:2016-06-13 14:35:01

标签: laravel laravel-5.2

我想在Laravel 5.2中编写一个可以上传照片的代码,我的问题是,一旦我开始上传很多照片,网站就会崩溃。

它给了我这个错误:VerifyCsrfToken.php第67行中的TokenMismatchException:

只有在表单中缺少{{csrf_field()}}时才会出现此错误,但在这种情况下它不是,它就在那里。

它与较少的图像完美配合。可能是什么问题?

控制器

public function store(Request $request)
{
    $rules = array(
        'picturess' => 'mimes:jpeg,jpg,bmp,png',
    );
    $messages = array(
        'mimes' => 'A feltöltetni kívánt kép nem felel meg a kritériumoknak. (Ilyen lehet a kép kiterjesztése: jpeg, jpg, bmp vagy png. A kép se lehet bármekkora.)',
        'integer' => 'A beírt szőveg nem szám.',
        'required' => 'Ennek a mezőnek a kitőltése kötelező.',
    );
    $validator = Validator::make(Input::all(), $rules, $messages);
    if ($validator->fails()) {
        $messages = $validator->messages();
        return Redirect::back()->withInput()->withErrors($validator);

    }
    if (Input::hasFile('pictures')) {
        $files = $request::file('pictures');
        $file_count = count($files);
        $uploadcount = 0;
        $destinationPath = 'uploads';
        $userId = Auth::user()->id;
        foreach ($files as $file) {
            if ($file->isValid()) {
                $extension = $file->getClientOriginalExtension();

                $pictureFileName = $this->makePictureFileName(0, $extension);
                $thumbnailPictureFileName = $this->makePictureFileName(1, $extension);
                Log::info('pictureFileName: '.$pictureFileName);

                if ($file->move($destinationPath, $pictureFileName)) {
                    $uploadcount++;
                    $img = Image::make($destinationPath . '/' . $pictureFileName);
                    //$img = Image::make($file->getClientOriginalName());
                    $img->resize(277, null, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                    $img->save($destinationPath . '/' . $thumbnailPictureFileName);
                    $picture = new Picture;
                    $picture->filename = $pictureFileName;
                    $picture->thumbnail_filename = $thumbnailPictureFileName;
                    $picture->user_id = $userId;
                    $picture->save();
                }

            } else {
                Session::flash('picture-error', 'A feltöltetni kívánt kép nem megfelelő. (Valószínűleg túl nagy.)');
                return redirect()->back()->withInput()->withErrors($validator);
            }
        }
        if ($uploadcount == $file_count) {
            Session::flash('success', 'A képek feltöltése sikeres.');
        } else {
            return redirect()->back()->withInput()->withErrors($validator);
        }
    }
    //return Redirect::to('pictures/create');
    return redirect()->back()->withInput()->withErrors($validator);
}

查看

@extends('layouts.site')

@section('content')
    <div class="row">
        <div class="col-md-12">

            {!! Form::open(array('method' =>'POST', 'url' => 'pictures', 'class' => 'uk-form', 'files'=> true)) !!}
            {{ csrf_field() }}


                <div class="form-group">
                    {!! Form::label('pictures', 'Kép', array('class' => '')) !!}
                    <div class="uk-form-controls">
                        {!! Form::file('pictures[]', array('class' => '','multiple'=>true)) !!}
                    </div>
                </div>

                <div class="form-group">
                    <button class="uk-button">Küldés</button>
                </div>


            @if ($errors->has('success'))
                <div class="alert alert-danger" role="alert">
                    <p>{{ $errors->first('success') }}</p>
                </div>
            @endif
            @if(Session::has('picture-error'))
                <div class="alert alert-danger" role="alert">
                    <p class="errors">{!! Session::get('error') !!}</p>
                </div>
            @endif
            @if (count($errors) > 0)
                <div class="alert alert-danger">
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif
            {!! Form::close() !!}

        </div>
    </div>
@stop

路线

Route::group(['middleware' => ['web']], function () {
    Route::get('/', 'SiteController@index');
    Route::get('admin', 'AdminController@index');
    Route::resource('pictures', 'PicturesController');
    Route::resource('users', 'UsersController');
    Route::auth();
});

1 个答案:

答案 0 :(得分:2)

{!! Form::open...会自动添加CSRF字段保护,因此您不需要此{{ csrf_field() }}