Laravel 5.2 多文件字段上传

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35640784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:18:07  来源:igfitidea点击:

Laravel 5.2 muliple file field upload

phpfilelaravelupload

提问by user3112038

Hi I have stumbled across a problem while messing around with Laravel 5.2 I dont know if the problem is specific for 5.2 or occurs in other versions of the framework.

嗨,我在使用 Laravel 5.2 时偶然发现了一个问题,我不知道该问题是否特定于 5.2 或出现在其他版本的框架中。

Basically what I'm trying to do is upload multiple files trough a single html file input field this is what I have.

基本上我想要做的是通过单个 html 文件输入字段上传多个文件,这就是我所拥有的。

The view:

风景:

<form method="post" action="nieuw" enctype="multipart/form-data">
    <input type="file" class="form-control" name="images" multiple>
</form>

The request file:

请求文件:

public function rules()
{
    return [
        'images' => 'mimes:jpg,jpeg,png'
    ];
}

And last but not least the controller:

最后但并非最不重要的控制器:

public function store(Requests\EventRequest $request)
{
    dd($request->file('images'));
}

Now when uploading multiple files trough the inpute field this is what the dump shows.

现在,当通过 inpute 字段上传多个文件时,这就是转储显示的内容。

UploadedFile {#30 ▼
  -test: false
  -originalName: "anotherone.jpg"
  -mimeType: "image/jpeg"
  -size: 64112
  -error: 0
  path: "/tmp"
  filename: "phpm57pCe"
  basename: "phpm57pCe"
  pathname: "/tmp/phpm57pCe"
  extension: ""
  realPath: "/tmp/phpm57pCe"
  aTime: 2016-02-25 23:46:57
  mTime: 2016-02-25 23:46:57
  cTime: 2016-02-25 23:46:57
  inode: 1443315
  size: 64112
  perms: 0100600
  owner: 900
  group: 900
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
}

It just seems to catch the last file u submitted trough the upload field. Note that the request validator does work at this point you can only upload the set mime types but its the request that fails to show all the files.

它似乎只是捕捉到您通过上传字段提交的最后一个文件。请注意,此时请求验证器确实可以工作,您只能上传设置的 mime 类型,但它的请求无法显示所有文件。

What I have figured out on my own is that setting the html input field into an array like this

我自己想出来的是将 html 输入字段设置为这样的数组

<form method="post" action="nieuw" enctype="multipart/form-data"> <input type="file" class="form-control" name="images[]" multiple> </form>

<form method="post" action="nieuw" enctype="multipart/form-data"> <input type="file" class="form-control" name="images[]" multiple> </form>

fixes the problem on the controller end making the dump look like this

修复了控制器端的问题,使转储看起来像这样

array:2 [▼
 0 => UploadedFile {#30 ▼
    -test: false
    -originalName: "test - kopie.jpg"
    -mimeType: "image/jpeg"
    -size: 64112
    -error: 0
    path: "/tmp"
    filename: "phpj3V7H1"
    basename: "phpj3V7H1"
    pathname: "/tmp/phpj3V7H1"
    extension: ""
    realPath: "/tmp/phpj3V7H1"
    aTime: 2016-02-25 23:52:21
    mTime: 2016-02-25 23:52:21
    cTime: 2016-02-25 23:52:21
    inode: 1443314
    size: 64112
    perms: 0100600
    owner: 900
    group: 900
    type: "file"
    writable: true
    readable: true
    executable: false
    file: true
    dir: false
    link: false
  }
  1 => UploadedFile {#31 ▼
    -test: false
    -originalName: "anotherone - kopie.jpg"
    -mimeType: "image/jpeg"
    -size: 4721735
    -error: 0
    path: "/tmp"
    filename: "phpWSm5U9"
    basename: "phpWSm5U9"
    pathname: "/tmp/phpWSm5U9"
    extension: ""
    realPath: "/tmp/phpWSm5U9"
    aTime: 2016-02-25 23:52:21
    mTime: 2016-02-25 23:52:21
    cTime: 2016-02-25 23:52:21
    inode: 1443315
    size: 4721735
    perms: 0100600
    owner: 900
    group: 900
    type: "file"
    writable: true
    readable: true
    executable: false
    file: true
    dir: false
    link: false
  }
]

However trough this method validation has completely gone out the window by doing so and i have tried changing my validator into:

然而,通过这样做,这种方法验证已经完全消失了,我已经尝试将我的验证器更改为:

public function rules()
{
    return [
        'images.*' => 'mimes:jpg,jpeg,png'
    ];
}

And into

并进入

public function rules()
{
    return [
        'images.0' => 'mimes:jpg,jpeg,png'
    ];
}

Both of them hopelessly failed. I hope some of you can help me out here. Thanks in advance!

他们俩都无可救药地失败了。我希望你们中的一些人可以帮助我。提前致谢!

回答by Froxz

There is no validation for arrayof iamges what you can do is

没有对图像array的验证,您可以做的是

$validator = \Validator::make($request->all(), [
  'images' => 'required'
]);
$files = $request->file('images');
foreach ($files as $file){
   $validator->after(function($validator) use ($file) {
      //make your valdiation
   });
}
if ($validator->fails()) {
   //validation didn't pass
}

The other way

另一种方法

$files = $request->file('images');
foreach ($files as $file){
   $input = ['upload' => $files[$i]];
   $rules = ['upload' => 'image|max:15500'];
   $validation = Validator::make($input, $rules);
   if($validator->fails()){
    //validation didn't pass
    break;
   }
}

A third way is to create your own Validation rule

第三种方法是创建自己的验证规则

回答by im_tsm

You can validate file array like any input array in Laravel 5.2. This feature is new in Laravel 5.2. You can do like following:

您可以像Laravel 5.2 中的任何输入数组一样验证文件数组。此功能是 Laravel 5.2 中的新功能。您可以执行以下操作:

$input_data = $request->all();

$validator = Validator::make(
    $input_data, [
    'image_file.*' => 'required|mimes:jpg,jpeg,png,bmp|max:20000'
    ],[
        'image_file.*.required' => 'Please upload an image',
        'image_file.*.mimes' => 'Only jpeg,png and bmp images are allowed',
        'image_file.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',
    ]
);

if ($validator->fails()) {
    // Validation error.. 
}

Note, you can also use $this->validate($request, $rules, $messages);inside a controller.

请注意,您也可以$this->validate($request, $rules, $messages);在控制器内部使用。