laravel 验证laravel 4中的输入文件图像数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19304708/
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 08:32:30  来源:igfitidea点击:

Validate input file image array in laravel 4

phpvalidationlaravellaravel-4

提问by Diego Castro

I have a form like this that can have multiples images[] field:

我有一个这样的表单,可以有多个图像 [] 字段:

{{ Form::open(array('url' => 'foo/bar', 'files' => true)) }}
    <input type="file" name="images[]">
{{ Form::close() }}

How can I validate this field with laravel 4 validation? I tried this rule but it didn't work:

如何使用 Laravel 4 验证来验证此字段?我试过这条规则,但没有奏效:

$rules = array('images[]' => 'required|image');

回答by Glad To Help

Try something like this:

尝试这样的事情:

$rules = array(
    'images'     => 'required|min:1|integerOrArray'
);

Based on the answer here

基于这里的答案

Use the validator from here

这里使用验证器

回答by Adam Desivi

First of all are you running "images[]" over a loop? There is plenty of code examples for laravel 4 that actually don't work at all... And people who wrote them don't know how to use var_dump()...

首先,您是否在循环中运行“图像 []”?有很多 Laravel 4 的代码示例实际上根本不起作用......编写它们的人不知道如何使用 var_dump()......

If not - your probably running validation over a array of objects, try check:

如果不是 - 您可能正在对一组对象进行验证,请尝试检查:

var_dump(Input::file('files'));

So now after this lecture, lets run a for loop (had some issue with foreach idk why):

所以现在在本次讲座之后,让我们运行一个 for 循环(为什么 foreach idk 有一些问题):

$files = Input::file('images');
$filesCount = count($files);

for ($i=0; $i < $filesCount; $i++)
{
    $file = $files[$i];
    $input = array(
        'upload' => $files[$i]
    );

    $rules = array(
        'upload' => 'image|max:15500'
    );
    $validation = Validator::make($input, $rules);
}

And now you can run your validation. Laravel4 only tries to valid mime type... so its vulnerable.

现在您可以运行您的验证。Laravel4 只尝试有效的 mime 类型......所以它很容易受到攻击。