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

废话不多说直接代码

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(); ?>




你可能感兴趣的:(yii上传图片,yii上传文件)