kohana框架上传文件验证规则写法示例,
kohana框架上传文件验证规则写法示例,
先声明下我用的是ko3.2.0版本.
kohana的验证,用的同学们都少懂点,因为每个函数的注释里都会举个例子.今天遇到的情况是验证图片上传,kohana的例子是这个样子的.
复制代码 代码如下:
$array->rule('file', 'Upload::type', array(array('jpg', 'png', 'gif')));
这个本身没有问题,但是呢,在实际的应用中总有点不方便,为什么呢,因为传到后面处理的时候,不仅要验证图片的上传还要验证form表单的某些字段.
一般我们会这样写
复制代码 代码如下:
$post = new Validation($_POST);
$file = new Validation($_FILES);
这样写也没什么问题,验证的时候按例子写也ok.但是觉得new2次有点怪怪的,而且我们也知道$_POST和$_FILES都是数组,可以一次验么?那肯定的,当然,我们得先把它们变成一个大的数组.这样写就OK.
复制代码 代码如下:
$post = new Validation(array_merge($_POST,$_FILES));//不明白的同学,度娘下array_merge
重点出场了,亲们.大家都知道验证form表单的字段写法和合并前没什么区别,关键是这个图片上传(或是其他的用到upload)要怎么写.
好吧,时间关系直接上代码,大家可以拿去直接用,当然有兴趣的同学也可以试用rules.
复制代码 代码如下:
$post->rule('img','not_empty')
->rule('img','Upload::type',array(':value',array('jpg','png','gif')))
->rule('img','Upload::size',array(':value','1M'));
PS:img就是前台form表单中input type="file" 那个控件名称啦,后台是找不到ID的.
再次声明我用的kohana3.2.0版本,其它版本注意修改下写法.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to upload files to 123 Cloud Disk? You can upload files to 123 Cloud Disk for storage, but most friends don’t know how to upload files to 123 Cloud Disk. Next is the picture and text of how to upload files to 123 Cloud Disk brought by the editor for players. Tutorial, interested users come and take a look! How to upload files on 123 Cloud Disk 1. First open 123 Cloud Disk and enter the main page, register or log in to the account; 2. Then enter the page as shown below, click the [Upload] button guided by the arrow; 3. Then the bottom will expand In the function bar window, click the [Select File] function; 4. Finally, select the file to be uploaded and wait patiently for the upload to complete.

Nut Cloud is an efficient file management tool dedicated to providing users with intelligent file saving and synchronization services. It has powerful data synchronization and backup functions to ensure that users' data is safe. A series of functions of Nut Cloud are designed to meet the needs of users in different scenarios and provide an excellent user experience. So how to upload files in the Nut Cloud app? This tutorial guide will give you a detailed introduction to the steps. I hope it can help everyone in need. How to upload files to Nut Cloud? 1. On the My Files page, click on My Nut Cloud to open it. 2. On the opened page, click the plus icon in the lower right corner. 3. In the options that pop up at the bottom, click Upload from SD card. 4. In the opened mobile phone storage, select the file.

How to handle upload file errors in PHP? In the actual web development process, file upload is a common functional requirement. However, when processing file uploads, various errors may occur, such as files exceeding size limits, file types not allowed, etc. This article will introduce how to handle errors in uploading files in PHP and provide specific code examples. File upload errors are usually divided into two broad categories: client-side errors and server-side errors. Client errors refer to problems that occur when users upload files, such as the uploaded file is too large, the file type is

With the continuous advancement of technology and the rapid development of the Internet, the demand for the development of high-performance applications is becoming stronger and stronger. In this context, developers are constantly looking for better frameworks and tools to improve application performance. Phalcon and Kohana are two popular PHP frameworks, both of which are considered good choices for high-performance application development. So, in the competition between Phalconvs and Kohana, which one is more suitable for high-performance application development? In the next article, we will delve into this issue

How to use Kohana framework with PHP? Kohana is a PHP framework based on MVC architecture, evolved from the original CodeIgniter framework. Its philosophy is simplicity, flexibility and elegance. The Kohana framework provides rich functionality and good reusability, making it the first choice for many PHP developers. So, how to use the Kohana framework? Here we briefly introduce how to install and use the Kohana framework. Installation First, we need to install it from Kohana official website

Kohana is a lightweight PHP framework developed using the MVC architectural style. It provides ORM (Object Relational Mapping) to simplify database operations and improve development efficiency. In this article, we will explore how to use ORM with Kohana framework. Configuring the database connection First, to use the ORM in the Kohana framework, we need to configure the database connection in the configuration file. Open the /application/config/database.php file and modify it

Form validation and error messages are important features in the Kohana framework. They can help us verify the validity of form data on the server side and provide friendly error messages to users. This article will explain how to use form validation and error messages in the Kohana framework. Define form validation rules in the controller The Kohana framework provides a powerful validation class Validation that can be used to verify the validity of form data. In the controller we can define form validation rules. For example, we have a

In today's Internet era, desktop applications have been slowly replaced by web applications. However, there are still many businesses and individuals who need to use desktop applications. So, when you choose a framework for building high-performance desktop applications, which framework should you choose? This article will explore Yii2 and Kohana, two frameworks to help you make better decisions. Yii2 and Kohana are both open source frameworks based on the PHP language. Yii2 is a high-performance web application development framework that provides comprehensive architecture and support
