request->isPost) { try { $file = UploadedFile::getInstanceByName($this->uploadFileName); if (!$file->getHasError()) { $stream = fopen($file->tempName, 'r+'); $path = '/runtime/pem/platform/' . md5(time()) .'.'. $file->getExtension();// 生成文件地址 $path = '/web/uploads/platform/' . md5(time()) .'.'. $file->getExtension();// 生成文件地址 $config = ['drive' => Attachment::DRIVE_LOCAL, 'alias_path' => Yii::getAlias('@backend')];// 重置配置 $this->uploadDrive = Yii::$app->uploadDrive->local($config); $this->filesystem = $this->uploadDrive->entity(); $result = $this->filesystem->writeStream($path, $stream); if (!$result) { throw new NotFoundHttpException('文件写入失败'); } if (is_resource($stream)) { fclose($stream); } } else { throw new NotFoundHttpException('上传失败,可能文件太大了'); } return $this->success('上传成功', ['path' => 'backend' . $path]); } catch (\Exception $e) { return $this->error('上传失败', $e->getMessage()); } } } /** * 微信js文件上传 * @Author: lun * @DateTime: 2022/7/26 0026 16:33 * @Copyright: copyright (c) 2021 广东七件事集团 * @return mixed|void */ public function actionWechatJs() { if (Yii::$app->request->isPost) { try { $file = UploadedFile::getInstanceByName($this->uploadFileName); if (!$file->getHasError()) { $h5_path = Yii::$app->basePath.'/../h5'; // 文件上传地址 $path_config = [ [ 'path' => '/web/'.$file->name,// 重置配置 'config' => ['drive' => Attachment::DRIVE_LOCAL, 'alias_path' => Yii::getAlias('@api')],// 重置配置 ], [ 'path' => '/web/'.$file->name,// 重置配置 'config' => ['drive' => Attachment::DRIVE_LOCAL, 'alias_path' => Yii::getAlias('@backend')],// 重置配置 ], [ 'path' => $file->name,// 重置配置 'config' => ['drive' => Attachment::DRIVE_LOCAL, 'alias_path' => $h5_path],// 重置配置 ], ]; foreach ($path_config as $item) { $stream = fopen($file->tempName, 'r+'); $item['config']['disable_asserts'] = true; $this->uploadDrive = Yii::$app->uploadDrive->local($item['config']); $this->filesystem = $this->uploadDrive->entity(); $config = $this->filesystem->getConfig(); $config->set('disable_asserts', true);// 设置覆盖上传 $result = $this->filesystem->writeStream($item['path'], $stream); if (!$result) { throw new NotFoundHttpException('文件写入失败'); } if (is_resource($stream)) { fclose($stream); } } } else { throw new NotFoundHttpException('上传失败,可能文件太大了'); } $this->success('上传成功', ['path' => $file->name]); } catch (\Exception $e) { return $this->error('上传失败', $e->getMessage()); } } } }