Quantcast
Channel: Live News for Yii Framework
Viewing all articles
Browse latest Browse all 3375

[Wiki] Multiple file upload handler

$
0
0

Hi,

Recently I've worked on a project where I had to upload multiple files. I've come up with a short generalized script. You can use it if you have a table with lot of file location fields/columns. I don't know if it's the best solution or not but it works great for me. so, lets start

in your controller you need to call myFileHandler function with only file upload fields.

so, it will be

public function actionIndex(){
 
........
........
 
  $model = $this->myFileHandler($model, array('logo','emailus_img','emailus_img_hover','more_img','more_img_hover','gomo_logo','mobile_phone_img','animate_on_mobile_img','animate_above_text_img','animate_under_text_img','bottom_right_img'));
 
.......
.......
}
public function myFileHandler($model, $imgFieldNameArr){
 
  foreach($imgFieldNameArr as $attribute){
    $instance = CUploadedFile::getInstance($model, $attribute);
 
    if($instance){
      $fullImgName = time().'_'.$attribute.'.'.$instance->getExtensionName();   
      $fullImgSource = Yii::getPathOfAlias('webroot').'/media/images/'.$fullImgName;    
 
      $instance->saveAs($fullImgSource);    
      $model->$attribute = $fullImgName;
    }
  }
  return $model; //return model with updated file path
}

I hope this little tricks will help you.


Viewing all articles
Browse latest Browse all 3375

Trending Articles