廢話很少說直接代碼
model
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('hits', 'numerical', 'integerOnly'=>
true),
array('title', 'length', 'max'=>80),
array('linkurl', 'length', 'max'=>255),
array('imgpath','file','types'=>'jpg,gif,png','on'=>'insert'),
array('thumb','file','types'=>'jpg,gif,png','on'=>'insert'),
array('addtime', 'length', 'max'=>10),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('aid, title, linkurl, addtime, hits', 'safe', 'on'=>'search'),
);
}
Controller 控制器
public function actionCreate()
{
$model=
new Slide;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Slide']))
{
$model->attributes=$_POST['Slide'];
$model->imgpath=CUploadedFile::getInstance($model,'imgpath');
$model->thumb=CUploadedFile::getInstance($model,'thumb');
if($model->imgpath)
{
$newimg = 'imgpath_'.time().'_'.rand(1, 9999).'.'.$model->imgpath->extensionName;
//根據時間戳重命名文件名,extensionName是獲取文件的擴展名
$model->imgpath->saveAs('assets/uploads/slide/'.$newimg);
$model->imgpath = 'assets/uploads/slide/'.$newimg;
//將p_w_picpath屬性從新命名
}
if($model->thumb)
{
$newthumb = 'thumb_'.time().'_'.rand(1, 9999).'.'.$model->thumb->extensionName;
$model->thumb->saveAs('assets/uploads/slide/'.$newthumb);
$model->thumb = 'assets/uploads/slide/'.$newthumb;
}
$model->addtime = time();
if($model->save())
$
this->redirect(array('view','id'=>$model->aid));
}
$
this->render('create',array(
'model'=>$model,
));
}
/**
* 修改
*/
public function actionUpdate($id)
{
$model=$
this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Slide']))
{
$model->attributes=$_POST['Slide'];
$model->imgpath=CUploadedFile::getInstance($model,'imgpath');
$model->thumb=CUploadedFile::getInstance($model,'thumb');
if($model->imgpath)
{
$newimg = 'imgpath_'.time().'_'.rand(1, 9999).'.'.$model->imgpath->extensionName;
//根據時間戳重命名文件名,extensionName是獲取文件的擴展名
$model->imgpath->saveAs('assets/uploads/slide/'.$newimg);
$model->imgpath = 'assets/uploads/slide/'.$newimg;
//將p_w_picpath屬性從新命名
}
else {
$model->imgpath = $_POST['imgpath2'];
}
if($model->thumb)
{
$newthumb = 'thumb_'.time().'_'.rand(1, 9999).'.'.$model->thumb->extensionName;
$model->thumb->saveAs('assets/uploads/slide/'.$newthumb);
$model->thumb = 'assets/uploads/slide/'.$newthumb;
}
else {
$model->thumb = $_POST['thumb2'];
}
$model->addtime = time();
if($model->save()) {
$
this->redirect(array('view','id'=>$model->aid));
}
}
$
this->render('update',array(
'model'=>$model,
));
}
view 視圖
<
?php $form=$this-
>beginWidget('CActiveForm', array(
'id'=>'slide-form',
'enableAjaxValidation'=>true,
'htmlOptions'=>array('enctype'=>'multipart/form-data')
)); ?>
<
table
width
="100%"
cellspacing
="0"
class
="table_form"
>
<
tbody
>
<
tr
>
<
th
width
="100"
>廣告標題:
</th>
<
td
>
<
?php echo $form-
>textField($model,'title',array('size'=>50,'maxlength'=>80)); ?>
<
?php echo $form-
>error($model,'title'); ?>
</td>
</tr>
<
tr
>
<
th
>連接地址:
</th>
<
td
>
<
?php echo $form-
>textField($model,'linkurl',array('size'=>50,'maxlength'=>255)); ?>
<
?php echo $form-
>error($model,'linkurl'); ?>
</td>
</tr>
</tbody>
</table>
<
div
style
="" id="p_w_picpathsdiv"
class
="pad-10"
>
<
fieldset
>
<
legend
>幻燈片設置
</legend>
<
table
width
="100%"
class
="table_form"
>
<
tbody
>
<
tr
>
<
th
width
="80"
>上傳圖片:
</th>
<
td
class
="y-bg"
style
="width:250px;"
>
<
?php echo CHtml::activeFileField($model,'imgpath'); ?
>
</td>
<
td
>
<
?php echo '<img src="'.$model-
>imgpath.'" width="20%"/>'; ?>
<
?php if(!$model-
>isNewRecord){?>
<
input
type
="hidden"
name
="imgpath2"
id
="hiddenField" value="<?php echo $model-
>imgpath;?>"/>
<
?php }?
>
</td>
</tr>
</tbody>
</table>
</fieldset>
</div>
<
div
id
="p_w_picpathsdiv"
class
="pad-10"
>
<
fieldset
>
<
legend
>縮略圖設置
</legend>
<
table
width
="100%"
class
="table_form"
>
<
tbody
>
<
tr
>
<
th
width
="80"
>上傳圖片:
</th>
<
td
class
="y-bg"
style
="width:250px;"
>
<
?php echo CHtml::activeFileField($model,'thumb'); ?
>
</td>
<
td
>
<
?php echo '<img src="'.$model-
>thumb.'" />'; ?>
<
?php if(!$model-
>isNewRecord){?>
<
input
type
="hidden"
name
="thumb2"
id
="hiddenField" value="<?php echo $model-
>thumb;?>"/>
<
?php }?
>
</td>
</tr>
</tbody>
</table>
</fieldset>
</div>
<
div
style
="margin-left:10px; line-height:30px;"
class
="bk15"
>
<
?php echo CHtml::submitButton($model-
>isNewRecord ? '肯定' : '修改',array('class'=>'button')); ?>
<
?php $this-
>endWidget(); ?>