Guest User

Untitled

a guest
Dec 13th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. protected function beforeValidate()
  2. {
  3.     $this->uploadFiles();
  4.  
  5.     if ($this->hasAttribute('author') && empty($this->author))
  6.         $this->author = Yii::app()->user->id;
  7.  
  8.     return parent::beforeValidate();
  9. }
  10.  
  11. protected function uploadFiles()
  12. {
  13.     foreach ($this->rules() as $rule) {
  14.         if (in_array("file", $rule)) {
  15.             $attributes = explode(",", $rule[0]);
  16.             foreach ($attributes as $attribute) {
  17.                 $image = CUploadedFile::getInstance($this, $attribute);
  18.  
  19.                 if (is_object($image)) {
  20.                     $filename = md5(rand(1, 99999) . $image) . $image;
  21.                     $image->saveAs('images/site/manufacturers/' . $filename);
  22.                     $this->$attribute = 'images/site/manufacturers/' . $filename;
  23.                 }
  24.             }
  25.         }
  26.     }
  27. }
Add Comment
Please, Sign In to add comment