Exemplo n.º 1
0
 public function upload()
 {
     if ($this->validate()) {
         $path = $this->getStoreInfo();
         $store_to = $path['real'] . $path['route'];
         if (!file_exists($store_to)) {
             FileHelper::createDirectory($store_to);
         }
         foreach ($this->files as $f) {
             $image = $this->isImageFile($f);
             $new_fn = $this->newFileName($f->baseName, $f->extension, $image);
             if ($f->saveAs($store_to . $new_fn)) {
                 $model = new Files();
                 if ($image) {
                     $model->attachBehavior('imageFileBehavior', ['class' => ImageBehavior::className()]);
                 }
                 $model->filename = $new_fn;
                 $model->base_dir = $path['real'];
                 $model->base_url = $path['web'];
                 $model->path = $path['route'];
                 $model->title = $f->baseName;
                 $model->alias = str_replace('.' . $f->extension, '', $new_fn);
                 $model->attach_table = $this->tableId;
                 $model->attach_id = $this->id;
                 if ($model->save()) {
                     $this->_uploadedIds[] = $model->id;
                 }
                 $this->_fn = $new_fn;
                 $this->_url = $path['web'] . $path['route'] . $new_fn;
             }
             //TODO обработка ошибки сохранения файла
         }
         return true;
     }
     return false;
 }