【问题标题】:File Upload Redirect with Error Not Display不显示错误的文件上传重定向
【发布时间】:2015-12-21 16:12:18
【问题描述】:

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


控制器

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

我是不是忘记了什么?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您需要稍微更改您的验证,以使其能够跟进@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 类型,以防您不想要它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-20
        • 2020-02-11
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 2017-01-16
        相关资源
        最近更新 更多