yii上传图片、yii上传文件、yii控件activeFileField使用
来源:互联网 发布:淘宝客推广有什么要求 编辑:程序博客网 时间:2024/05/16 01:50
model
Controller 控制器
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'),
);
}
{
// 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;
//将image属性重新命名
}
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;
//将image属性重新命名
} 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,
));
}
{
$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;
//将image属性重新命名
}
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;
//将image属性重新命名
} 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="imagesdiv" class="pad-10">
<fieldset>
<legend>幻灯片设置</legend>
<table width="100%" class="table_form">
<tbody>
<tr>
<thwidth="80">上传图片:</th>
<tdclass="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){?>
<inputtype="hidden"name="imgpath2"id="hiddenField" value="<?php echo $model->imgpath;?>"/>
<?php }?>
</td>
</tr>
</tbody>
</table>
</fieldset></div>
<div id="imagesdiv" class="pad-10">
<fieldset>
<legend>缩略图设置</legend>
<table width="100%" class="table_form">
<tbody>
<tr>
<thwidth="80">上传图片:</th>
<tdclass="y-bg"style="width:250px;"><?php echo CHtml::activeFileField($model,'thumb'); ?></td>
<td>
<?php echo '<img src="'.$model->thumb.'" />'; ?>
<?php if(!$model->isNewRecord){?>
<inputtype="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(); ?>
'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="imagesdiv" class="pad-10">
<fieldset>
<legend>幻灯片设置</legend>
<table width="100%" class="table_form">
<tbody>
<tr>
<thwidth="80">上传图片:</th>
<tdclass="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){?>
<inputtype="hidden"name="imgpath2"id="hiddenField" value="<?php echo $model->imgpath;?>"/>
<?php }?>
</td>
</tr>
</tbody>
</table>
</fieldset></div>
<div id="imagesdiv" class="pad-10">
<fieldset>
<legend>缩略图设置</legend>
<table width="100%" class="table_form">
<tbody>
<tr>
<thwidth="80">上传图片:</th>
<tdclass="y-bg"style="width:250px;"><?php echo CHtml::activeFileField($model,'thumb'); ?></td>
<td>
<?php echo '<img src="'.$model->thumb.'" />'; ?>
<?php if(!$model->isNewRecord){?>
<inputtype="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(); ?>
文章来源:http://wuhai.blog.51cto.com/blog/2023916/953300
0 0
- yii上传图片、yii上传文件、yii控件activeFileField使用
- yii上传图片、yii上传文件、yii控件activeFileField使用
- yii上传图片、yii上传文件、yii控件activeFileField使用
- yii上传图片、yii上传文件、yii控件activeFileField使用
- yii上传图片、yii上传文件、yii控件activeFileField使用
- yii上传图片、yii上传文件、yii控件activeFileField使用
- Yii上传图片,上传文件,yii控件activefilefield的使用
- Yii中使用activeFileField上传图片注意事项
- yii中使用activeFileField实现图片的上传
- yii 图片上传
- yii上传图片
- YII 上传一个图片
- yii上传图片
- yii图片上传教程
- Yii多图片上传
- yii框架图片上传
- Yii框架图片上传
- yii 上传图片
- 用mysql数据库写的分词算法代码
- bool 和BOOL的区别
- 读取笔记本的摄像头的原始yuv数据,通过libav(ffmpeg编码)
- Git 过滤文件,控制上传
- 不常用到的sql语句记录
- yii上传图片、yii上传文件、yii控件activeFileField使用
- vi
- 对象左列表增加非对象关联表的字段,添加两个以上字段
- plsql developer中 保存/删除 登录的用户名和密码
- 不定期搬运二次元福利
- hibernate查询
- Java多线程总结之线程安全队列Queue
- 每一位Android开发者应该知道的Android体系架构和开发库
- Ajax的优势与不足