Laravel 5.2 - VerifyCsrfToken.php第67行中的TokenMismatchException:

时间:2016-06-28 14:18:07

标签: php forms laravel upload laravel-5.2

我正在创建一个视频上传系统,当我提交视频时,它会向我显示:

  

VerifyCsrfToken.php第67行中的TokenMismatchException

我认为这是:



<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet"/>
<form method="POST" action="{{ url('video/upload') }}" enctype="multipart/form-data" class="form-horizontal">
            {{ csrf_field() }}
            <fieldset class="form-group">
                <label for="title">Title</label>
                <input type="text" id="title" name="title" class="form-control">
            </fieldset>
            <fieldset class="form-group">
                <label for="video">Video</label>
                <input type="file" id="video" name="video" class="form-control">
            </fieldset>
            <fieldset class="form-group text-lg-right">
                <button class="btn btn-success" type="submit">Upload</button>
            </fieldset>
        </form>
&#13;
&#13;
&#13;

这在我的控制器中:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Video;

use App\Http\Requests;

class VideoController extends Controller
{
    public function upload()
    {
        return view('video.upload');
    }

    public function uploadPost(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|unique:videos|max:100',
            'tags' => 'required',
            'video' => 'required'
        ]);

        $mime = $request->file('video')->getMimeType();

        dd($mime);
    }
}

我的php.ini是:

  

upload_max_filesize = 100M

因此,我有php.ini,csrf_field()但仍然出错。可能是什么问题?

3 个答案:

答案 0 :(得分:0)

{{ csrf_field() }}更改为{!! csrf_field() !!}

默认情况下,Laravel将转义注入模板的所有数据。通过将csrf_field标记中的{!! !!}的调用包围起来,您告诉Laravel 转义csrf_field()函数生成的HTML标记。

&#13;
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet"/>
<form method="POST" action="{{ url('video/upload') }}" enctype="multipart/form-data" class="form-horizontal">
            {!! csrf_field() !!}
            <fieldset class="form-group">
                <label for="title">Title</label>
                <input type="text" id="title" name="title" class="form-control">
            </fieldset>
            <fieldset class="form-group">
                <label for="video">Video</label>
                <input type="file" id="video" name="video" class="form-control">
            </fieldset>
            <fieldset class="form-group text-lg-right">
                <button class="btn btn-success" type="submit">Upload</button>
            </fieldset>
        </form>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

post_max_size也需要引发,因为它会引发POST Content-Length错误。

答案 2 :(得分:-1)

这可能会对你有所帮助

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

在表单中添加