yii中使用activeFileField实现图片的上传
来源:互联网 发布:购物网站源码java 编辑:程序博客网 时间:2024/05/16 01:26
在YII中要实现图片的上传,可以使用activeFileField
1、在VIEW中:
enctype这句话一定要加上去。
<?php $form=$this->beginWidget('CActiveForm', array('id'=>'tbl-goods-form',// 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, //要实现图片的上传与修改,enctype这句话一定要加上去。 'htmlOptions'=>array('enctype'=>'multipart/form-data'))); ?>
2、在VIEW中:
<div class="row"><?php echo $form->labelEx($model,'goods_small_pic'); ?><?php //echo $form->textField($model,'goods_small_pic',array('size'=>60,'maxlength'=>255)); ?><?php echo CHtml::activeFileField($model,'goods_small_pic'); ?> <?php echo $form->error($model,'goods_small_pic'); ?> <!--预览图,并且设置一个隐藏域,用来暂存修改前的URL信息--> <?php echo '<img src="'.$model->goods_small_pic.'" width="100px"/>'; ?> <?php if(!$model->isNewRecord){?> <input type="hidden" name="temp_img2" id="hiddenField" value="<?php echo $model->goods_small_pic;?>"/> <?php }?></div>
3、模型中:
public function rules(){// NOTE: you should only define rules for those attributes that// will receive user inputs.return array(array('is_sale, is_delete, is_best, is_new, is_hot, is_promote', 'numerical', 'integerOnly'=>true),array('cat_id, click_count, brand_id, goods_number, market_price, shop_price, promote_price', 'length', 'max'=>8),array('goods_sn', 'length', 'max'=>16),array('goods_name', 'length', 'max'=>32),//array('goods_desc, goods_small_pic, goods_big_pic', 'length', 'max'=>255), array('goods_desc','length','max'=>255), //要实现图片的上传与修改,要将保存图片URL地址的字段改为file,如下所示。 array('goods_small_pic','file','types'=>'jpg,gif,png','on'=>'insert'), array('goods_big_pic','file','types'=>'jpg,gif,png','on'=>'insert'), array('promote_start_date, promote_end_date, add_time', 'safe'),// The following rule is used by search().// @todo Please remove those attributes that should not be searched.array('goods_id, cat_id, goods_sn, goods_name, click_count, brand_id, goods_number, market_price, shop_price, promote_price, promote_start_date, promote_end_date, goods_desc, goods_small_pic, goods_big_pic, is_sale, is_delete, is_best, is_new, is_hot, is_promote, add_time', 'safe', 'on'=>'search'),);}
4、控制器中:
public function actionCreate(){$model=new TblGoods;// Uncomment the following line if AJAX validation is needed// $this->performAjaxValidation($model); //使用下拉菜单,实现添加产品或修改产品时,可以从下拉列表选择产品类别:以下为第1步 //在Goods控制器下实例化category对象,并执行SQL语句,取得所有数据。 $category = TblCategory::model()->findAll();if(isset($_POST['TblGoods'])){$model->attributes=$_POST['TblGoods']; //实现文件,图片的上传 $model->goods_small_pic= CUploadedFile::getInstance($model, goods_small_pic); $model->goods_big_pic= CUploadedFile::getInstance($model, goods_big_pic); if($model->goods_small_pic){ $newimg='goods_small_pic_'.time().'_'.rand(1,9999).".".$model->goods_small_pic->extensionName; //$newimg="abc.jpg"; $model->goods_small_pic->saveAs('assets/uploads/tblgoods/'.$newimg); $model->goods_small_pic='assets/uploads/tblgoods/'.$newimg; } if($model->goods_big_pic){ $newimg2='goods_big_pic_'.time().'_'.rand(1,9999).".".$model->goods_big_pic->extensionName; //$newimg2="abcd.jpg"; $model->goods_big_pic->saveAs('assets/uploads/tblgoods/'.$newimg2); $model->goods_big_pic='assets/uploads/tblgoods/'.$newimg2; } if($model->save())$this->redirect(array('view','id'=>$model->goods_id));}$this->render('create',array('model'=>$model, //使用下拉菜单,实现添加产品或修改产品时,可以从下拉列表选择产品类别:以下为第2步 //在Goods控制器下create将上面得到的数组,分配到create这个VIEW页面。 'category'=>$category));}/** * 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); // //使用下拉菜单,实现添加产品或修改产品时,可以从下拉列表选择产品类别:以下为第1步 //在Goods控制器下实例化category对象,并执行SQL语句,取得所有数据。 $category = TblCategory::model()->findAll();if(isset($_POST['TblGoods'])){$model->attributes=$_POST['TblGoods']; //实现文件,图片的上传 $model->goods_small_pic= CUploadedFile::getInstance($model, goods_small_pic); $model->goods_big_pic= CUploadedFile::getInstance($model, goods_big_pic); if($model->goods_small_pic){ $newimg='goods_small_pic_'.time().'_'.rand(1,9999).".".$model->goods_small_pic->extensionName; //$newimg="abc.jpg"; $model->goods_small_pic->saveAs('assets/uploads/tblgoods/'.$newimg); $model->goods_small_pic='assets/uploads/tblgoods/'.$newimg; }else{ $model->goods_small_pic=$_POST['temp_img1']; } if($model->goods_big_pic){ $newimg2='goods_big_pic_'.time().'_'.rand(1,9999).".".$model->goods_big_pic->extensionName; //$newimg2="abcd.jpg"; $model->goods_big_pic->saveAs('assets/uploads/tblgoods/'.$newimg2); $model->goods_big_pic='assets/uploads/tblgoods/'.$newimg2; }else{ $model->goods_big_pic=$_POST['temp_img2']; } if($model->save())$this->redirect(array('view','id'=>$model->goods_id));}$this->render('update',array('model'=>$model, //使用下拉菜单,实现添加产品或修改产品时,可以从下拉列表选择产品类别:以下为第2步 //在Goods控制器下create将上面得到的数组,分配到create这个VIEW页面。 'category'=>$category));}
0 0
- yii中使用activeFileField实现图片的上传
- Yii中使用activeFileField上传图片注意事项
- 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上传文件、yii控件activeFileField使用
- yii中上传图片总结
- yii中上传图片总结
- YII CHTML::activeFileField 上传下载
- Yii 使用七牛上传图片
- jfinal中使用百度编辑器上传图片的实现
- Rails中 使用carrierwave实现图片上传
- YII+DWZ框架,使用swfupload实现上传
- yii 图片上传
- yii上传图片
- 自动合并单元格
- java Timer的使用以及schedult的理解
- 动画效果-animation
- mysql 截取逗号分隔前一个
- (function(){...}())javascript中为何在匿名function函数后面还外加一个括号
- yii中使用activeFileField实现图片的上传
- 设置 Linux 的 LD_LIBRARY_PATH 变量
- iOS searchbar拼音和汉字搜索全国城市
- STL 队列
- ASA常用简单命令
- Longest increasing sub sequence poj 1080
- java map 遍历方式的选择
- iOS 7 教程:定制iOS 7中的导航栏和状态栏
- 用OpenSceneGraph实现的NeHe OpenGL教程 - 第十六课