2015-12-21 49 views
0

我不确定我的文件上传的原因是什么。它失败并重新导回,但是不与错误文件上传重定向错误不显示


控制器

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();!!} 

我是不是忘了什么东西?

回答

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类型。

3

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