How Yii uses model to upload multiple files

Source: Internet
Author: User
I used to upload pictures using Cformmodel (leaflet). For example, Registerform to register users to upload photos and use thumbnails:


' Email format is not valid '), array (' Password ', ' required '), Array (' Gender ', ' in ', ' range ' =>array (0,1)), Array (' Homeland ', ' required ', ' message ' = ' home required, and not easy to change '), Array (' Homeland ', ' validatehome '), arr Ay (' imei ', ' Required ', ' message ' = = ' imei required '), array (' Birthday ', ' required ', ' message ' = ' birthday Required '), Array (' Avatar ', ' file ', ' message ' + ' must set an Avatar ')); }/** * declares attribute labels. */Public Function attributelabels () {return array (' email ' + ' username/email/mobile number/roaming number ', ' pas Sword ' + ' password ', ' gender ' = ' gender ', '); }/** * Authenticates the password. * the ' authenticate ' validator as declared in rules (). */Public function authenticate ($attribute, $params) {if (! $this->haserrors ()) {$this-&G T;_identity=new useridentity ($this->username, $this->password); if (! $this->_identity->authenticate ()) $this->adderror (' Password ', ' incorrect username or password. '); }}/** * Verify that your hometown is legal. Extended to the prefecture-level cities * * */Public Function validatehome () {$this->homeland; $this->adderror (' Homeland ', ' home 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 returnFalse }}

Then inside 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_big '. $preRand. $registerForm->avatar->extensionname;                $registerForm->avatar->saveas (' uploads/' $imageName);            $registerForm->avatar = $imageName; } $path = DirName (Yii::app ()->basepath).            '/uploads/'; $thumb = Yii::app ()->thumb;            What is the difference from the $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 (),//save method automatically verifies if ($registerForm->save () && $registerForm-&gt Login ()) {Yii::app ()->db->getlastinsertid ();//Gets the ID of the insertion. But}}

The main point of uploading is here:

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

But now there's a problem. Is $registerform inherited the Cformmodel, this save method is Cactiverecord, why is the Save method called?

Also, if I want to upload more files, isn't

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

You can do it?

Reply content:

I used to upload pictures using Cformmodel (leaflet). For example, Registerform to register users to upload photos and use thumbnails:


' Email format is not valid '), array (' Password ', ' required '), Array (' Gender ', ' in ', ' range ' =>array (0,1)), Array (' Homeland ', ' required ', ' message ' = ' home required, and not easy to change '), Array (' Homeland ', ' validatehome '), arr Ay (' imei ', ' Required ', ' message ' = = ' imei required '), array (' Birthday ', ' required ', ' message ' = ' birthday Required '), Array (' Avatar ', ' file ', ' message ' + ' must set an Avatar ')); }/** * declares attribute labels. */Public Function attributelabels () {return array (' email ' + ' username/email/mobile number/roaming number ', ' pas Sword ' + ' password ', ' gender ' = ' gender ', '); }/** * Authenticates the password. * the ' authenticate ' validator as declared in rules (). */Public function authenticate ($attribute, $params) {if (! $this->haserrors ()) {$this-&G T;_identity=new useridentity ($this->username, $this->password); if (! $this->_identity->authenticate ()) $this->adderror (' Password ', ' incorrect username or password. '); }}/** * Verify that your hometown is legal. Extended to the prefecture-level cities * * */Public Function validatehome () {$this->homeland; $this->adderror (' Homeland ', ' home 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 returnFalse }}

Then inside 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_big '. $preRand. $registerForm->avatar->extensionname;                $registerForm->avatar->saveas (' uploads/' $imageName);            $registerForm->avatar = $imageName; } $path = DirName (Yii::app ()->basepath).            '/uploads/'; $thumb = Yii::app ()->thumb;            What is the difference from the $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 (),//save method automatically verifies if ($registerForm->save () && $registerForm-&gt Login ()) {Yii::app ()->db->getlastinsertid ();//Gets the ID of the insertion. But}}

The main point of uploading is here:

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

But now there's a problem. Is $registerform inherited the Cformmodel, this save method is Cactiverecord, why is the Save method called?

Also, if I want to upload more files, isn't

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

You can do it?

Hello, you. Did you solve the problem with this multi-file upload?

  • 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.