コード例 #1
0
 /**
  * @param array $uploaderConfig
  * @param array $fileConfig
  * @return \app\file\models\File[]
  * @throws \yii\base\InvalidConfigException
  */
 public function upload($uploaderConfig = [], $fileConfig = [])
 {
     /** @var BaseUploader $uploader */
     $uploader = \Yii::createObject(ArrayHelper::merge(['class' => empty($_FILES) ? '\\app\\file\\uploaders\\PutUploader' : '\\app\\file\\uploaders\\PostUploader', 'destinationDir' => $this->filesRootPath, 'maxFileSize' => $this->fileMaxSize], $uploaderConfig));
     if (!$uploader->upload()) {
         return ['errors' => $uploader->getFirstErrors()];
     }
     $files = [];
     foreach ($uploader->files as $item) {
         $file = new File();
         $file->attributes = ArrayHelper::merge($fileConfig, ['uid' => $item['uid'], 'title' => $item['title'], 'fileName' => $item['name'], 'fileMimeType' => $item['type'], 'fileSize' => $item['bytesTotal']]);
         if (!$file->save()) {
             return ['errors' => $uploader->getFirstErrors()];
         }
         $files[] = $file;
     }
     return $files;
 }
コード例 #2
0
 protected function getFiles()
 {
     $value = $this->model[$this->attribute] ?: [];
     if (empty($value)) {
         return [];
     }
     if (is_string($value)) {
         $value = StringHelper::explode($value);
     }
     $value = $this->multiple ? $value : [$value[0]];
     return array_map(function ($fileModel) {
         /** @var File $fileModel */
         return $fileModel->getExtendedAttributes();
     }, File::findAll($value));
 }
コード例 #3
0
 public function actionIndex($uid)
 {
     /** @var File $file */
     $file = File::findOne($uid);
     if (!$file) {
         throw new NotFoundHttpException();
     }
     if (FileModule::getInstance()->xHeader !== false) {
         \Yii::$app->response->xSendFile($file->path, $file->downloadName, ['xHeader' => FileModule::getInstance()->xHeader]);
     } else {
         header('Content-Description: File Transfer');
         header('Content-Type: application/octet-stream');
         header('Content-Disposition: attachment; filename="' . $file->getDownloadName() . '"');
         header('Expires: 0');
         header('Cache-Control: must-revalidate');
         header('Pragma: public');
         header('Content-Length: ' . filesize($file->path));
         readfile($file->path);
     }
 }