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-> 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-> 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?