文章编辑页面用户可以不上传图片,就是用原来的图片,可是会提示一定要上传图片 [ 1.1 版本 ]
model
public function rules()
{
return array(
array('title', 'required','message'=>'标题必填'),
array('cid', 'check_cid'),
array('type', 'in','range'=>array(0,1),'message'=>'请选择类型'),
array('info', 'required','message'=>'摘要必填'),
array('thumb','file','types'=>'jpg,gif,png,jpeg,bmp', 'wrongType'=>'类型不符','wrongMimeType'=>'类型不符','message'=>'没有上传'),
array('content', 'required','message'=>'内容必填'),
);
}
控制器add
public function actionAdd()
{
$articleModel = new Article();
$categoryModel = category::model();
$categoryList = $categoryModel-> findAll();
$list=array();
$list[]='请选择栏目';
foreach ($categoryList as $cat) {
$list[$cat->cid]=$cat->cname;
}
if(isset($_POST['Article'])){
$articleModel->attributes=$_POST['Article'];
$img = CUploadedFile::getInstance($articleModel, 'thumb');
if(is_object($img) && $img instanceof CUploadedFile){
$pre='img_'.time().mt_rand(10000, 99999);
$ext=$img->extensionName;
$articleModel->thumb=$pre.'.'.$ext;
}else{
$articleModel->thumb='no.jpg';
}
$articleModel->time=time();
if($articleModel->save()){
if(is_object($img) && $img instanceof CUploadedFile){
$img->saveAs('uploads/' . $pre.'.'.$ext);
}
Yii::app()->user->setFlash('success','添加成功');
$this->redirect(array('index'));
}else{
Yii::app()->user->setFlash('success','添加失败');
}
}
$this->render('add',array('articleModel'=>$articleModel,'list'=>$list));
}
控制器update
public function actionEdit($aid)
{
$articleModel = Article::model();
$articleinfo = $articleModel->findByPk($aid);
$categoryModel = category::model();
$categoryList = $categoryModel-> findAll();
$list=array();
$list[]='请选择栏目';
foreach ($categoryList as $cat) {
$list[$cat->cid]=$cat->cname;
}
if(isset($_POST['Article'])){
$origimg = $articleinfo->thumb;
$articleinfo->attributes=$_POST['Article'];
$img = CUploadedFile::getInstance($articleModel, 'thumb');
if(is_object($img) && $img instanceof CUploadedFile){
$pre='img_'.time().mt_rand(10000, 99999);
$ext=$img->extensionName;
$articleinfo->thumb=$pre.'.'.$ext;
}else{
$articleinfo->thumb=$origimg;
}
$articleinfo->time=time();
if($articleinfo->save()){
if(is_object($img) && $img instanceof CUploadedFile){
$img->saveAs('uploads/' . $pre.'.'.$ext);
}
Yii::app()->user->setFlash('success','编辑成功');
$this->redirect(array('index'));
}else{
Yii::app()->user->setFlash('success','编辑失败');
}
}
$this->render('edit',array('articleinfo'=>$articleinfo,'list'=>$list));
}
共 4 个回答
-
lgw2008521 回答于 2015-05-03 13:55 举报
//保存原图,如上传新图片则替换,否则保存原图
$oldThumb = $model->thumb ? $model->thumb : null;
if(isset($_POST['Article'])) { $model->attributes=$_POST['Article']; //缩图上传 $fileInst = CUploadedFile::getInstance($model, 'thumb'); if ($fileInst && in_array($fileInst->type, array('image/gif','image/jpeg','image/pjpeg','image/png','image/x-png'))) { } else { $model->thumb = isset($_POST['Article']['thumbD']) && $_POST['Article']['thumbD'] ? null : $oldThumb; $model->thumbD = isset($_POST['Article']['thumbD']) ? $_POST['Article']['thumbD'] : 0; } }
-
lgw2008521 回答于 2015-05-03 13:57 举报
我原来做过一个文章管理,就是按照上面来做的,没有问题。你试试。其中,
['thumbD']
判断编辑时是否删除原图的 -
lgw2008521 回答于 2015-05-03 13:59 举报
rules()规则中缩图相关不需要设置。
wozjd
注册时间:2015-04-30
最后登录:2016-12-06
在线时长:2小时41分
最后登录:2016-12-06
在线时长:2小时41分
- 粉丝0
- 金钱20
- 威望0
- 积分40