Exemplo n.º 1
0
 /**
  * @return \app\modules\document\models\Attachment|bool
  * @throws \yii\base\Exception
  */
 public function upload()
 {
     if ($this->validate()) {
         $root = \Yii::getAlias('@webroot');
         $path = '/uploads/' . date('Y-m-d');
         $uploadPath = $root . $path;
         $file = $this->file;
         //check for the existence of a folder
         if (!file_exists($uploadPath)) {
             FileHelper::createDirectory($uploadPath, 0777, true);
         }
         //check for file with same name is exists
         $srcPath = $path . '/' . $file->name;
         $savePath = $uploadPath . '/' . $file->name;
         if (file_exists($savePath)) {
             $t = time();
             $savePath = $uploadPath . '/' . $t . "_" . $file->name;
             $srcPath = $path . '/' . $t . "_" . $file->name;
         }
         //move uploaded file and save it into database
         if ($file->saveAs($savePath)) {
             return Attachment::createNew($file, $srcPath);
         }
     }
     return false;
 }