我以前是使用CFormModel來(lái)上傳圖片(單張)。比如RegisterForm來(lái)注冊(cè)用戶上傳照片并使用縮略圖:
<?php
class RegisterForm extends CFormModel
{
public $email;
public $password;
public $gender;
public $homeland;
public $iemi;
public $registerDate;
public $wentWhere;
public $birthday;
public $avatar; //頭像,其最終結(jié)果應(yīng)該是存儲(chǔ)的Url
public $thumb;//縮略圖
private $_identity;
public function rules()
{
return array(
array('email', 'required'),
array('email','email','message'=>'email的格式不合法'),
array('password', 'required'),
array('gender','in','range'=>array(0,1)),
array('homeland','required','message'=>'家鄉(xiāng)必填,而且不容易更改'),
array ('homeland','validateHome'),
array('imei','required','message'=>'imei必填'),
array('birthday','required','message'=>'birthday必填'),
array('avatar','file','message'=>'必須設(shè)置一個(gè)頭像')
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'email'=>'用戶名/郵箱/手機(jī)號(hào)/漫游號(hào)',
'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.');
}
}
/**
* 驗(yàn)證家鄉(xiāng)是否合法。擴(kuò)展到地級(jí)市
*
*
*/
public function validateHome(){
$this->homeland;
$this->addError('homeland','家鄉(xiāng)不合法啊');
}
/**
* 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()有什么區(qū)別?
$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方法會(huì)自動(dòng)驗(yàn)證
if ($registerForm->save() && $registerForm->login()) {
Yii::app()->db->getLastInsertID(); //取得插入的Id。但是
}
}
上傳的時(shí)候主要是這里:
$registerForm->avatar = CUploadedFile::getInstance($registerForm, 'avatar');
但是現(xiàn)在遇到了一個(gè)問(wèn)題。就是$registerForm繼承了CFormModel,這個(gè)save方法是CActiveRecord的,為什么save方法就調(diào)用了呢?
此外,如果我要多文件上傳,是不是
$file1=CUploadedFile::getInstance($registerForm, 'file1');
$file2=CUploadedFile::getInstance($registerForm, 'file2');
就可以了?