Social Icons

Pages

Monday, April 22, 2013

Multiple Image Uploading



step-1:

In view

    <div class="row">
        <?php echo $form->labelEx($model,'image'); ?>
        <?php
          $this->widget('CMultiFileUpload', array(
             'model'=>$model,
             'name'=>'image',
             'attribute'=>'image',
             'accept'=>'jpg|gif|png',
            ));
        ?>
        <?php echo $form->error($model,'image'); ?>
    </div>


in controller at actionCreate

public function actionCreate()
{
        $model=new Photo;
        // Uncomment the following line if AJAX validation is needed
         $this->performAjaxValidation($model);

        if(isset($_POST['Photo']))
        {
            $model->attributes=$_POST['Photo'];
            $images = CUploadedFile::getInstancesByName('image');
           
            if(isset($images) && count($images)> 0)
            {
                foreach ($images as $image=>$pic)
                {
                                if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/images/'.$pic->name,0777))     
                    {  
                        $model->setIsNewRecord(true);
                        $model->id = null;
                        $model->image = $pic->name;
                        $model->save();
                    }              
                }
                        $this->redirect(array('view','id'=>$model->id));
            }
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }

1 comment:

  1. It is very stupid example. Please do not copy if you do not understand.
    Where is :
    $model=new Photo;
    If you do not have model Photo, how can yii understand?

    ReplyDelete