Controller代码:(包括增、改、删)
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new CompanyNews;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['CompanyNews']))
{
$model->attributes=$_POST['CompanyNews'];
if(empty($_POST['CompanyNews']['news_pic'])){
$model->news_pic = $model->news_pic;
}
$file = CUploadedFile::getInstance($model,'news_pic'); //获得一个CUploadedFile的实例
if(is_object($file)&&get_class($file) === 'CUploadedFile'){ // 判断实例化是否成功
$model->news_pic = './assets/upfile/News_file_'.time().'_'.rand(0,9999).'.'.$file->extensionName; //定义文件保存的名称
}else{
$model->news_pic = './assets/upfile/nopic.jpg'; // 若果失败则应该是什么图片
}
if($model->save()){
if(is_object($file)&&get_class($file) === 'CUploadedFile'){
$file->saveAs($model->news_pic); // 上传图片
}
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['CompanyNews']))
{
$imageurl = $model->news_pic;
$file = CUploadedFile::getInstance($model,'news_pic'); //获得一个CUploadedFile的实例
if(is_object($file)&&get_class($file) === 'CUploadedFile'){ // 判断实例化是否成功
$model->news_pic = './assets/upfile/News_file_'.time().'_'.rand(0,9999).'.'.$file->extensionName; //定义文件保存的名称
}
else{
$model->news_pic = $imageurl; // 若果失败则应该是什么图片
}
if($model->save()){
if(is_object($file)&&get_class($file) === 'CUploadedFile'){
$file->saveAs($model->news_pic); // 上传图片
//删除旧图片
if(is_file($imageurl))
{
unlink($imageurl);
}
}
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('update',array(
'model'=>$model,
));
}
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'admin' page.
* @param integer $id the ID of the model to be deleted
*/
public function actionDelete($id)
{
$model=$this->loadModel($id);
$imageurl = $model->news_pic;
if(is_file($imageurl))
{
unlink($imageurl);
}
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
View:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'company-news-form',
'htmlOptions'=>array('enctype'=>'multipart/form-data'),//上传图片,所以要增加这一句属性。
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
)); ?>
<div class="row">
<?php echo $form->labelEx($model,'news_pic'); ?>
<?php //echo $form->textField($model,'news_pic',array('size'=>60,'maxlength'=>100)); ?>
<?php echo CHtml::activeFileField($model,'news_pic'); ?>
<?php echo $form->error($model,'news_pic'); ?>
</div>
顺便说一句,CSDN的代码高亮好烂啊。