【问题标题】:Cannot upload picture in laravel 4无法在laravel 4中上传图片
【发布时间】:2015-03-21 14:42:42
【问题描述】:

我有这个基本形式:

{{ Form::open(array('url' => URL::route('post-account-changeProfilePic'), 'files' => true, ))}}
{{ Form::file('photo') }}
<br />
{{ Form::submit('Regístrarme', array("class" => "button expand round")) }}
{{ Form::close() }}

我的路线在 2 个组内:before=&gt;Authbefore=&gt;csrf

Route::post('/accont/changeProfilePic', array(
    'as'    => 'post-account-changeProfilePic',
    'uses'  =>  'CallCenterController@postChangeProfilePic'
));

在我的控制器中,我只是转储我的变量以查看我得到了什么:

public function postChangeProfilePic(){
    $input = Input::all();
    var_dump($input);
}

这些是我得到的错误:

1- 照亮\会话\TokenMismatchException。

这是因为 csrf 过滤器,但由于我使用的是刀片,因此令牌实际上就在那里。另外,如果我从文件输入中删除 name 属性,则不会显示此错误。

到目前为止,我决定将路由放在 csrf 过滤器之外,直到我了解发生了什么。

2- 将路由放在 csrf 过滤器之外,并尝试显示所有输入后,我得到一个 null 数组。

我决定添加一个新的文本字段,如果我不选择照片/图片而只发送这样的表单,它将在屏幕上转储所有输入,但当然,文件是空的/null .

知道我做错了什么吗?

【问题讨论】:

    标签: php laravel-4 image-uploading


    【解决方案1】:

    在您的表单中尝试将其更改为 数组('之前' => 'csrf'),函数()

    {{ Form::open(array('url' => URL::route('post-account-changeProfilePic'), 'before' => 'csrf'), 'files' => true, ))}}
    

    默认情况下 csrf 令牌应该在那里,因为您正在执行 POST 请求,因此不确定那里存在问题。

    http://laravel.com/docs/4.2/html#csrf-protection

    对于输入尝试

    public function postChangeProfilePic(){
    
        if (Input::hasFile('photo'))
        {
            $input= Input::file('photo');
            var_dump($input);
        }
    }
    

    http://laravel.com/docs/4.2/requests#files

    【讨论】:

    • 感谢您的回复克里斯。问题仍然存在。我在一个组中有几条路线,例如: Route::group(array('before' => 'csrf'), function(){ //Routes here... });是的,令牌是由框架放置在表单中的。帖子数组一直“发布”为空......所以不知道该怎么办
    • 好的,你能为我添加另一个表单输入字段吗?只是一个文本输入并确认该输入也没有发布任何内容?
    • 我确实做到了。我在表单中添加了一个文本字段,当我发布它时,数组会显示所有字段(令牌+文本字段+文件),但前提是文件输入为空。如果文件输入不为空,它会发布“null”。
    【解决方案2】:

    好吧,我想可能是这样

    改变

    {{ Form::open(array('url' => URL::route('post-account-changeProfilePic'),
                            'files' => true, ))}}
    

    {{ Form::open(array('route' => 'post-account-changeProfilePic',
                            'files' => true )) }}
    

    我认为你也不需要 true 末尾的 ','。

    【讨论】:

      猜你喜欢
      • 2014-12-22
      • 2014-09-18
      • 2014-05-11
      • 2020-03-21
      • 2020-10-03
      • 2018-04-17
      • 2017-08-01
      • 1970-01-01
      • 2015-03-13
      相关资源
      最近更新 更多