限制可以上传的文件数 [英] Limit number of files that can be uploaded

查看:117
本文介绍了限制可以上传的文件数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何限制可以上传的文件数量?

How can I limit the number of files that can be uploaded?

max验证似乎适用于图像的大小(以千字节为单位).如何验证允许上传的最大文件数(例如,一次输入只能上传10个文件)?

The max validation seems to apply to the size of the image (in kilobytes). How can I make a validation for the maximum number of files allowed to be uploaded (for example, only 10 files can be uploaded from a single input)?

推荐答案

在laravel中,没有内置的验证规则.但是您可以创建自定义验证规则来处理.

In laravel, there is no built-in validation rule for that. But you can create custom-validation rule to handle this.

这是一个简单的自定义验证规则.

Here is a simple custom-validation rule for it.

app/目录中创建customValidator.php.

Validator::extend('upload_count', function($attribute, $value, $parameters)
{   
    $files = Input::file($parameters[0]);

    return (count($files) <= $parameters[1]) ? true : false;
});

别忘了将其添加到app/start/global.php

require app_path().'/customValidator.php';

在您的验证设置中,

$messages = array(
    'upload_count' => 'The :attribute field cannot be more than 3.',
);

$validator = Validator::make(
    Input::all(),
    array('file' => array('upload_count:file,3')), // first param is field name and second is max count
    $messages
);

if ($validator->fails()) {
     // show validation error
}

希望对您有用.

这篇关于限制可以上传的文件数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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