Home  >  Article  >  Backend Development  >  Example of how to write verification rules for uploading files in the kohana framework, _PHP tutorial

Example of how to write verification rules for uploading files in the kohana framework, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:20767browse

Example of writing verification rules for uploading files in kohana framework,

First of all, let me state that I am using version ko3.2.0.

Students who use kohana have little understanding of the verification, because there are examples in the comments of each function. The situation I encountered today was to verify the upload of images, and the example of kohana looked like this.

Copy code The code is as follows:
$array->rule('file', 'Upload::type', array(array('jpg ', 'png', 'gif')));

There is no problem with this in itself, but it is always a bit inconvenient in actual applications. Why? Because when it is passed to later processing, not only the upload of the image must be verified, but also certain fields of the form must be verified.
Normally we would write like this
Copy code The code is as follows:

$post = new Validation($_POST);
$file = new Validation($_FILES);

There is nothing wrong with writing like this, and it is OK to write according to the example when verifying. But it feels a bit strange to use new twice, and we also know that $_POST and $_FILES are both arrays, can they be verified at once? Yes, of course, we have to First turn them into a large array. This is OK.
Copy code The code is as follows:

$post = new Validation(array_merge($_POST,$_FILES));//For students who don’t understand, download array_merge

The key point is here, dear friends. Everyone knows that the way to write the fields in the verification form is no different from before the merger. The key is how to write the image upload (or other upload).
Okay, time-related, I will go directly to the code, and you can use it directly. Of course, interested students can also try out the rules.
Copy code The code is as follows:

$post->rule('img','not_empty')
->rule('img','Upload::type',array(':value',array('jpg','png','gif')))
->rule('img','Upload::size',array(':value','1M'));

PS: img is the name of the control with input type="file" in the front-end form. The ID cannot be found in the back-end.
Let me reiterate that I am using version kohana 3.2.0. For other versions, please be careful to modify the writing.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840641.htmlTechArticleExample of writing verification rules for uploading files in the kohana framework. Let me first state that I am using the ko3.2.0 version. Verification of kohana , students who use it are less familiar with it, because there are examples in the comments of each function...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn