tz_com 2011-03-14 18:58:47 11007次浏览 29条回复 1 1 0

在开发中常会遇到批量上传图片的情况,有一些拓展当然可以实现,但是我不会用…… 今天用Yii内置的СMultiFileUpload实现了,把代码发给大家供大家参考,请大家指出我的不足之处~ PS:这里主要是上传,具体存入数据库,请先自行考虑,如有疑问直接问我吧~嘿嘿……够懒吧~ Controller:

public function actionCreate()
{
  $model=new Upload;
  if(isset($_POST['Upload']))
  {
    $model->attributes= $_POST['Upload'];
    $model->image =  CUploadedFile::getInstancesByName('image');         
    foreach ($model->image as $file) {
      $file->saveAs('images/upload/' .$model->image->name);
    } 
    $this->redirect(array('view','id'=>$model->id));
  }   
  $this->render('create',array('model'=>$model));
} 

View:

<div class="form">
  <?php echo CHtml::beginForm('','post',array('enctype'=>'multipart/form-data')); ?>      
    <div class="row">
      <?php echo CHtml::activeLabelEx($model,'image'); ?>
      <?php //echo CHtml::activeFileField($model, 'image');
      $this->widget('CMultiFileUpload', array(
        'name' => 'image',
        'max' => 2,
        'accept' => 'jpg|gif|png',
      ));
  ?>  
<div class="row buttons">
  <?php echo CHtml::submitButton( '提交'); ?>
</div>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->
觉得很赞
您需要登录后才可以回复。登录 | 立即注册