【问题标题】:Laravel 5.2 - TokenMismatchException in VerifyCsrfToken.php line 67:Laravel 5.2 - VerifyCsrfToken.php 第 67 行中的 TokenMismatchException:
【发布时间】:2016-10-30 22:20:23
【问题描述】:

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

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>

这在我的控制器中:

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() 并且仍然出现错误。可能是什么问题?

【问题讨论】:

  • 对 php.ini 进行更改后是否重新启动了 Web 服务器?
  • 你的post_max_size 是什么?
  • 原来是 8M,我把它改成了 100M,效果很好。将其发布为答案:D 谢谢。

标签: php forms laravel upload laravel-5.2


【解决方案1】:

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

默认情况下,Laravel 会转义注入到模板中的任何数据。通过在 {!! !!} 标记中包围对 csrf_field 的调用,您是在告诉 Laravel not 转义由 csrf_field() 函数生成的 HTML 标记。

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

【讨论】:

  • 请在 {!! csrf_field() !!} 应该类似于
  • 两种方式都一样!
  • 除了值属性其他应该相同。
  • 还有另一种方法,但它在安全性方面不好。您可以通过在 App\Http\Middleware\VerifyCsrfToken.php 文件 protected $except = [ 'video/upload' ]; 中添加此代码,在验证令牌中间件中指定排除 'video/upload' 路由中的令牌检查
  • 请检查 php.ini 文件中的 post_max_size 变量值,它应该是 post_max_size=100M
【解决方案2】:

这可能对你有帮助

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

将此添加到您的表单中

【讨论】:

  • 你看到他的表格了吗? {{ csrf_field() }} 它做同样的事情
【解决方案3】:

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

【讨论】:

  • 这是绝对的解决方案,因为它是 8M,我已将其与 upload_max_filesize 一起更改为 100M。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 2017-09-24
  • 1970-01-01
  • 2016-07-27
  • 2016-04-24
  • 1970-01-01
相关资源
最近更新 更多