How does Yii use model to upload multiple files?

Source: Internet
Author: User
I used to use CFormModel to upload images (single sheet ). For example, RegisterForm is used to register a user to upload a photo and use a thumbnail: {code ...} then in Controller: {code ...} during the upload process, the following code is mainly used: {code ...} but now we have a problem. It is $ r... I used to use CFormModel to upload images (single sheet ). For example, RegisterForm is used to register a user to upload a photo and use a thumbnail:


'Invalid email format '), array ('Password', 'required'), array ('gender', 'in ', 'range' => array (), array ('homeland', 'required', 'message' => 'hometown required, and not easy to change '), array ('homeland', 'validatehome'), array ('imei', 'required', 'message' => 'imei required '), array ('birthday ', 'requestred', 'message' => 'birthday required '), array ('Avatar', 'file ', 'message' => 'An Avatar must be set');}/*** Declares attribute labels. */public function attributeLabels (){ Return array ('email '=> 'user name/email/mobile phone number/Roaming number', 'Password' => 'Password', 'gender' => 'gender ', '');}/*** Authenticates the password. * This is the 'authenticate' validator as declared in rules (). */public function authenticate ($ attribute, $ params) {if (! $ This-> hasErrors () {$ this-> _ identity = new UserIdentity ($ this-> username, $ this-> password); if (! $ This-> _ identity-> authenticate () $ this-> addError ('Password', 'recorrect username or password. ') ;}}/*** verify that your hometown is legal. Extended to prefecture-level cities ***/public function validateHome () {$ this-> homeland; $ this-> addError ('homeland', 'hometown is illegal ');} /*** Logs in the user using the given username and password in the model. * @ return boolean whether login is successful */public function login () {if ($ this-> _ identity = null) {$ this-> _ identity = new UserIdentity ($ this-> username, $ this-> password); $ this-> _ identity-> setPersistentStates (array ()); $ this-> _ identity-> authenticate ();} if ($ this-> _ identity-> errorCode = UserIdentity: ERROR_NONE) {$ duration = 3600*24*10; // 10 days Yii: app ()-> user-> login ($ this-> _ identity, $ duration ); return true;} else return false ;}}

Then in the Controller:

Public function actionRegister () {$ registerForm = new RegisterForm (); if (isset ($ _ POST ['registerform']) {$ registerForm-> attributes =$ _ POST ['registerform']; $ RegisterForm-> avatar = CUploadedFile: getInstance ($ registerForm, 'Avatar '); if ($ registerForm-> avatar) {$ preRand = time (). mt_rand (0, 99999); $ imageName = 'img _ day '. $ preRand. $ registerForm-> avatar-> extensionName; $ registerForm-> avatar-> saveA S ('uploads /'. $ imageName); $ registerForm-> avatar = $ imageName;} $ path = dirname (Yii: app ()-> BasePath ). '/uploads/'; $ thumb = Yii: app ()-> thumb; // What is the difference between it and $ thumb = new Cthumb? $ Thumb-> image = $ path. 'img _ small '. $ preRand. $ registerForm-> avatar-> extensionName; $ thumb-> width = 130; $ thumb-> height = 95; $ thumb-> mode = 4; $ thumb-> directory = $ path; $ thumb-> defaultName = $ preRand; $ thumb-> createThumb (); $ thumb-> save (); $ registerForm-> thumb = $ thumb-> image; $ registerForm-> registerDate = time (); // The save method automatically verifies if ($ registerForm-> save () & $ registerForm-> login () {Yii: app ()-> Db-> getLastInsertID (); // obtain the inserted Id. But }}

This is the main part of the upload process:

$registerForm->avatar = CUploadedFile::getInstance($registerForm, 'avatar');

But now we have a problem. $ RegisterForm inherits CFormModel. The save method is CActiveRecord. Why is the save method called?

In addition, if I want to upload multiple files, is it true?

$file1=CUploadedFile::getInstance($registerForm, 'file1');$file2=CUploadedFile::getInstance($registerForm, 'file2');

That's it?

Reply content:

I used to use CFormModel to upload images (single sheet ). For example, RegisterForm is used to register a user to upload a photo and use a thumbnail:


'Invalid email format '), array ('Password', 'required'), array ('gender', 'in ', 'range' => array (), array ('homeland', 'required', 'message' => 'hometown required, and not easy to change '), array ('homeland', 'validatehome'), array ('imei', 'required', 'message' => 'imei required '), array ('birthday ', 'requestred', 'message' => 'birthday required '), array ('Avatar', 'file ', 'message' => 'An Avatar must be set');}/*** Declares attribute labels. */public function attributeLabels (){ Return array ('email '=> 'user name/email/mobile phone number/Roaming number', 'Password' => 'Password', 'gender' => 'gender ', '');}/*** Authenticates the password. * This is the 'authenticate' validator as declared in rules (). */public function authenticate ($ attribute, $ params) {if (! $ This-> hasErrors () {$ this-> _ identity = new UserIdentity ($ this-> username, $ this-> password); if (! $ This-> _ identity-> authenticate () $ this-> addError ('Password', 'recorrect username or password. ') ;}}/*** verify that your hometown is legal. Extended to prefecture-level cities ***/public function validateHome () {$ this-> homeland; $ this-> addError ('homeland', 'hometown is illegal ');} /*** Logs in the user using the given username and password in the model. * @ return boolean whether login is successful */public function login () {if ($ this-> _ identity = null) {$ this-> _ identity = new UserIdentity ($ this-> username, $ this-> password); $ this-> _ identity-> setPersistentStates (array ()); $ this-> _ identity-> authenticate ();} if ($ this-> _ identity-> errorCode = UserIdentity: ERROR_NONE) {$ duration = 3600*24*10; // 10 days Yii: app ()-> user-> login ($ this-> _ identity, $ duration ); return true;} else return false ;}}

Then in the Controller:

Public function actionRegister () {$ registerForm = new RegisterForm (); if (isset ($ _ POST ['registerform']) {$ registerForm-> attributes =$ _ POST ['registerform']; $ RegisterForm-> avatar = CUploadedFile: getInstance ($ registerForm, 'Avatar '); if ($ registerForm-> avatar) {$ preRand = time (). mt_rand (0, 99999); $ imageName = 'img _ day '. $ preRand. $ registerForm-> avatar-> extensionName; $ registerForm-> avatar-> saveA S ('uploads /'. $ imageName); $ registerForm-> avatar = $ imageName;} $ path = dirname (Yii: app ()-> BasePath ). '/uploads/'; $ thumb = Yii: app ()-> thumb; // What is the difference between it and $ thumb = new Cthumb? $ Thumb-> image = $ path. 'img _ small '. $ preRand. $ registerForm-> avatar-> extensionName; $ thumb-> width = 130; $ thumb-> height = 95; $ thumb-> mode = 4; $ thumb-> directory = $ path; $ thumb-> defaultName = $ preRand; $ thumb-> createThumb (); $ thumb-> save (); $ registerForm-> thumb = $ thumb-> image; $ registerForm-> registerDate = time (); // The save method automatically verifies if ($ registerForm-> save () & $ registerForm-> login () {Yii: app ()-> Db-> getLastInsertID (); // obtain the inserted Id. But }}

This is the main part of the upload process:

$registerForm->avatar = CUploadedFile::getInstance($registerForm, 'avatar');

But now we have a problem. $ RegisterForm inherits CFormModel. The save method is CActiveRecord. Why is the save method called?

In addition, if I want to upload multiple files, is it true?

$file1=CUploadedFile::getInstance($registerForm, 'file1');$file2=CUploadedFile::getInstance($registerForm, 'file2');

That's it?

Hello. Have you solved this problem of uploading multiple files?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.