使用哑剧验证Laravel文件发布-Word文件 [英] Using Mimes for Validating Laravel File Post - Word File

查看:118
本文介绍了使用哑剧验证Laravel文件发布-Word文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个张贴文件的表格.我正在尝试使用验证仅接受Word文档.我尝试使用mime类型,但似乎无法正常工作,而且我也无法发现自己​​的错误.

I have a form that I post file. I am trying to use validation to accept only word documents. I tried using mime types however doesn't seem to work and I couldn't spot my mistake.

<form action="" method="post">
   <div class="form-group{{ $errors->has('myFile') ? ' has-error' : '' }}">
      <div class="col-xs-12">
           <div class="form-material">
               <input class="form-control" type="file" id="myFile" name="myFile">
               <label for="myFile">MyFile</label>
               @if ($errors->has('myFile'))
                    <div {{ $errors->first('myFile') }}</div>
               @endif
           </div>
      </div>
   </div>
</form>

此发布成功,并且我在我的控制器中收到了它.

This posts successfully and I receive it in my controller.

在我的控制器中,我尝试对其进行验证,但是即使文件为'.doc'格式,它也总是返回错误.

In my controller, I try to validate it, however it is always returning the error, even though the file is in '.doc' format.

public function myController(Request $request) {
     $validator = MyValidations::myFormValidator($request->all());

     if ($validator->fails()) {
          $this->throwValidationException(
               $request, $validator
          );
     }

     ...
}

和验证者:

public static function myFormValidator($data) {
    return Validator::make($data, [
        'myFile' => 'required|mimes:application/msword'

        // I also tried
        // 'myFile' => 'required|mimes:doc,docx'
    ]);
}


关于SO,我看到了一些帖子,关于文件config > mimes.php,但是Laravel 5.3上没有它.


I have seen some posts on SO, regarding that there is a file config > mimes.php but I don't have it on Laravel 5.3

推荐答案

实际上,您的验证代码没有真正的问题.问题在于您的表单未正确提交文件.

Actually, there is nothing really wrong with your validation code. The problem is that your form is not submitting the files properly.

将适当的enctype添加到表单中,如下所示:

Add the proper enctype to your form like so:

<form action="" method="post" enctype="multipart/form-data">

还要确保正确使用语法进行验证(但我想您知道这部分).使用以下两种方法之一:

Also make sure to get the syntax right for validation (but I think you knew this part). Use either of these two:

'myFile' => 'required|mimes:doc,docx'
'myFile' => 'required|mimetypes:application/msword'

这篇关于使用哑剧验证Laravel文件发布-Word文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆