/**
  * @param $filePath string
  * @param
  *            $owner
  *
  * @return bool|File
  *
  * @throws \Exception
  * @throws \yii\base\InvalidConfigException
  */
 public function attachFile($file)
 {
     $filePath = $file['path'];
     $attachment = new Attachment();
     $attachment->uploadFromFile($filePath);
     $attachment->name = $file['name'];
     if ($attachment->save()) {
         return $attachment;
     } else {
         return false;
         // 存储失败的话的处理
     }
 }
예제 #2
0
 public function saveLogo($path)
 {
     $attachment = new Attachment();
     $attachment->uploadFromFile($path);
     $attachment->save();
     $attachmentIndex = new AttachmentIndex();
     $attachmentIndex->attachment_id = $attachment->primaryKey;
     $attachmentIndex->entity = get_class($this);
     $attachmentIndex->entity_id = 0;
     $attachmentIndex->attribute = "appLogoImage";
     $attachmentIndex->save();
     return $attachment;
 }
예제 #3
0
 public function saveAvatar($avatar)
 {
     if ($this->avatarImage) {
         $this->avatarImage->delete();
     }
     $attachment = new Attachment();
     $attachment->uploadFromFile($avatar);
     $attachment->save();
     $attachmentIndex = new AttachmentIndex();
     $attachmentIndex->attachment_id = $attachment->primaryKey;
     $attachmentIndex->entity = get_class($this);
     $attachmentIndex->entity_id = $this->primaryKey;
     $attachmentIndex->attribute = "avatarImage";
     $attachmentIndex->save();
 }
예제 #4
0
 /**
  * @return array
  * @throws \HttpException
  */
 public function run()
 {
     $result = [];
     $uploadedFiles = UploadedFile::getInstancesByName($this->fileparam);
     foreach ($uploadedFiles as $uploadedFile) {
         /* @var \yii\web\UploadedFile $uploadedFile */
         $output = [];
         $output['id'] = -1;
         $output[$this->responseUrlParam] = "";
         $output['thumbnailUrl'] = "";
         $output['deleteUrl'] = "";
         $output['updateUrl'] = "";
         $output['path'] = "";
         if ($uploadedFile->error === UPLOAD_ERR_OK) {
             $validationModel = DynamicModel::validateData(['file' => $uploadedFile], $this->validationRules);
             if (!$validationModel->hasErrors()) {
                 $attachment = new Attachment();
                 if ($this->storageLocation == static::STORAGE_LOCATION_TEMPPATH) {
                     $attachment->setStorageDirectory($attachment->getTempDirectory());
                     $attachment->uploadFromPost($uploadedFile);
                 } else {
                     if ($this->storageLocation == static::STORAGE_LOCATION_USERPATH_DATABASE) {
                         $attachment->uploadFromPost($uploadedFile);
                         $attachment->save();
                     }
                 }
                 $output = array_merge($output, $attachment->toArray());
                 if ($attachment->primaryKey) {
                     $output["id"] = $attachment->primaryKey;
                 }
                 $output["path"] = $attachment->getPath();
                 $output[$this->responseUrlParam] = $attachment->getUrl();
                 $output["thumbnailUrl"] = $attachment->getUrl();
                 $output["deleteUrl"] = Url::to(array_merge($this->deleteUrl, ['path' => $output["path"], 'id' => $output['id']]));
                 $output["updateUrl"] = Url::to(array_merge($this->updateUrl, ['path' => $output["path"], 'id' => $output['id']]));
             } else {
                 $output['error'] = true;
                 $output['errors'] = $validationModel->errors;
             }
         } else {
             $output['error'] = true;
             $output['errors'] = $this->resolveErrorMessage($uploadedFile->error);
         }
         $result["files"][] = $output;
     }
     $result = $this->multiple ? $result : array_shift($result);
     return $result;
 }