一些表单验证的规则php
array('title','required','message'=>'标题必填'), array('type','in','range'=>array(0,1),'message'=>'请选择类型'),//范围 array('cid','自定义函数'), array('thumb','file','types'=>'jpg,gif,png,jpeg','message'=>'没有上传或者类型不对'),
上传类:app
$model = new model(); $model->thumb = CUploadedFile::getInstance($model,'thumb'); if($model->thumb){ $preRand = 'img_'.time().mt_rand(0,9999);//随机的前缀 $imgName = $preRand.'.'.$model->thumb->extensionName;//连上文件格式 $model->thumb->saveAs('uploads/'.$imgName);//保存位置 $model->thumb = $imgName; } //缩略图 $path = dirname(Yii::app()->BasePath).'/uploads/'; $thumb = Yii::app()->thumb;//引入类 $thumb->image = $path.$imgName;//要缩略的图片 $thumb->width = 130; $thumb->height = 95; $thumb->mode =4;//缩略类型 $thumb->directory = $path;//保存路径 $thumb->defaultName = $preRand;//默认名字,而且不保存原图 $thumb->createThumb();//建立缩略图 $thumb->save();//保存缩略图
扩展缩略图类(ext表明protected/extensions):函数
1.在extension中创建CThumb/CThumb.php文件 2.在main.php里面配置 'components' => array( 'thumb' => array( 'class' => 'ext.CThumb.CThumb'//路径别名 ) )