yii上传图片、yii上传文件、yii控件activeFileField使用

3 篇文章 0 订阅
2 篇文章 0 订阅
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;
         //将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 >
         < 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 ="imagesdiv"   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();


原创作品: http://wuhai.blog.51cto.com/2023916/953300

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值