文件上载重定向错误不显示

时间:2015-12-21 16:12:19

标签: php laravel redirect laravel-5 laravel-5.1

我不确定文件上传的原因是什么。它失败并重定向,但没有错误

控制器

public function updateLogo()
{

    $inputs = Input::all();
    $logo_path = Input::only('logo_path');
    $cpe_mac = $inputs['cpe_mac'];
    $rule =  ['logo_path' => 'mimes:jpeg,bmp,png'];

    $validator = Validator::make($logo_path, $rule );

    if ( $validator->fails()) {
        return Redirect::to($cpe_mac.'/view-profile/')->withErrors($validator)->withInput();
    } else {

    ..... 

    }

查看

{!! Form::open(array('url' => '/'.$cpe_mac.'/view-profile/logo/update', 'class' => 'form-horizontal', 'role' =>'form','id' => 'editLogo','file'=>true)) !!}

  <input name="logo_path" type="file" required> <br><br>

   <button class="btn btn-success btn-sm mr5" type="file"><i class="fa fa-user"></i> Update Logo</button>

{!! Form::hidden('cpe_mac', $cpe_mac)!!}
{{ csrf_field() }}
{!! Form::close();!!}

我忘记了什么吗?

2 个答案:

答案 0 :(得分:3)

可以使用Input::file()调用文件(真正上传的文件,而不仅仅是字符串);它未包含在标准Input::all()中。

答案 1 :(得分:1)

您需要稍微更改您的验证才能使其能够跟进@marmorunl答案。

public function updateLogo()
{
    $input = [
        'logo_path' => Input::file('logo_path')
    ];

    $rules = [
        'logo_path' => 'mimes:jpeg,bmp,png|required'
    ];

    $validator = Validator::make($input, $rules);

    if ($validator->fails()) {
         return Redirect::to($cpe_mac.'/view-profile/')->withErrors($validator)->withInput()
    } else {
        // else
    }
};

有关其他信息,请参阅Laravel: Validate an uploaded file is an image。这表明使用image作为验证规则,其中还包括git和svg。所以我把它留作你使用的mime类型,以防你不想要这个。