'email的格式不合法'), array('password', 'required'), array('gender','in','range'=>array(0,1)), array('homeland','required','message'=>'家乡必填,而且不容易更改'), array ('homeland','validatehome'), array('imei','required','message'=>'imei必填'), array('birthday','required','message'=>'birthday必填'), array('avatar','file','message'=>'必须设置一个头像') ); } /** * declares attribute labels. */ public function attributelabels() { return array( 'email'=>'用户名/邮箱/手机号/漫游号', 'password'=>'密码', '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','incorrect username or password.'); } } /** * 验证家乡是否合法。扩展到地级市 * * */ public function validatehome(){ $this->homeland; $this->adderror('homeland','家乡不合法啊'); } /** * 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; }}
然后在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; //与 $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方法会自动验证 if ($registerform->save() && $registerform->login()) { yii::app()->db->getlastinsertid(); //取得插入的id。但是 } }
上传的时候主要是这里:
$registerform->avatar = cuploadedfile::getinstance($registerform, 'avatar');
但是现在遇到了一个问题。就是$registerform继承了cformmodel,这个save方法是cactiverecord的,为什么save方法就调用了呢?
此外,如果我要多文件上传,是不是
$file1=cuploadedfile::getinstance($registerform, 'file1');$file2=cuploadedfile::getinstance($registerform, 'file2');
就可以了?
回复内容: 我以前是使用cformmodel来上传图片(单张)。比如registerform来注册用户上传照片并使用缩略图:
'email的格式不合法'), array('password', 'required'), array('gender','in','range'=>array(0,1)), array('homeland','required','message'=>'家乡必填,而且不容易更改'), array ('homeland','validatehome'), array('imei','required','message'=>'imei必填'), array('birthday','required','message'=>'birthday必填'), array('avatar','file','message'=>'必须设置一个头像') ); } /** * declares attribute labels. */ public function attributelabels() { return array( 'email'=>'用户名/邮箱/手机号/漫游号', 'password'=>'密码', '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','incorrect username or password.'); } } /** * 验证家乡是否合法。扩展到地级市 * * */ public function validatehome(){ $this->homeland; $this->adderror('homeland','家乡不合法啊'); } /** * 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; }}
然后在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; //与 $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方法会自动验证 if ($registerform->save() && $registerform->login()) { yii::app()->db->getlastinsertid(); //取得插入的id。但是 } }
上传的时候主要是这里:
$registerform->avatar = cuploadedfile::getinstance($registerform, 'avatar');
但是现在遇到了一个问题。就是$registerform继承了cformmodel,这个save方法是cactiverecord的,为什么save方法就调用了呢?
此外,如果我要多文件上传,是不是
$file1=cuploadedfile::getinstance($registerform, 'file1');$file2=cuploadedfile::getinstance($registerform, 'file2');
就可以了?
你好啊。这个多文件上传的这问题你解决了吗?