Yii——图片附件的上传、编辑及删除【同样适用于其它类型附件】
来源:互联网 发布:淘宝风干牛肉干可靠吗? 编辑:程序博客网 时间:2024/06/05 02:11
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的代码高亮好烂啊。
- Yii——图片附件的上传、编辑及删除【同样适用于其它类型附件】
- C# 上传附件 删除附件
- 上传附件,附件类型问题
- 给C++初学者的50个忠告——同样适用于PHP初学者
- ueditor对附件上传类型的控制
- 脚本录制和参数化时的问题及技巧——上传附件
- Discuz x2 设置上传附件大小及类型
- discuz如何设置上传附件大小及类型
- destoon 上传图片后删除上传的附件
- 魅族M8游戏框架开发的思路及方法,同样适用于WinCE设备的游戏开发。
- 一些同样适用于人生的计算机原理
- C#——基于委托与事件的多线程通信(同样适用于非UI线程间通信)
- yii CGridView的查看、编辑、删除按钮的url修改
- uploadify 上传图片附件问题
- jmeter上传视频图片附件
- 附件上传,下载,查看,删除
- 上传单个附件,修改,删除
- Dedecms删除文档同时删除图片及附件的方法
- 用hg server 来发布Mercurial repository
- Linux学习1--Linux在VMware Workstation虚拟机中的超详细安装(RHEL_5.7)
- 将IP以整型的形式存储到数据库中
- shell递归遍历目录
- HLG 1400 汽车比赛
- Yii——图片附件的上传、编辑及删除【同样适用于其它类型附件】
- win7 下的telnet不是内部或外部命令也不是可运行的程序或批处理
- c# winform 实现对postgresql数据库的自动备份还原功能
- QT的定时器事件中遇见的一个误区
- iOS 中json解析数据出现中文乱码的问题
- 全球身份ID
- 恭喜我漂亮的小宝贝
- 程序员最该看的30本书 --------国外编辑推荐
- leetcode Swap Nodes in Pairs java实现