daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest Dec 13th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top