FileController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace backend\modules\admin\controllers;
  3. use backend\controllers\AdminController;
  4. use common\models\common\Attachment;
  5. use \Yii;
  6. use yii\web\NotFoundHttpException;
  7. use yii\web\UploadedFile;
  8. class FileController extends AdminController
  9. {
  10. public $uploadFileName = 'file';
  11. protected $uploadDrive;
  12. /**
  13. *
  14. * @var \League\Flysystem\Filesystem
  15. */
  16. protected $filesystem;
  17. /**
  18. * 微信文件单独上传
  19. * @Author: lun
  20. * @DateTime: 2021/11/23 0023 15:12
  21. * @Copyright: copyright (c) 2021 广东七件事集团
  22. * @return mixed|void
  23. */
  24. public function actionUpload()
  25. {
  26. if (Yii::$app->request->isPost) {
  27. try {
  28. $file = UploadedFile::getInstanceByName($this->uploadFileName);
  29. if (!$file->getHasError()) {
  30. $stream = fopen($file->tempName, 'r+');
  31. $path = '/runtime/pem/platform/' . md5(time()) .'.'. $file->getExtension();// 生成文件地址
  32. $path = '/web/uploads/platform/' . md5(time()) .'.'. $file->getExtension();// 生成文件地址
  33. $config = ['drive' => Attachment::DRIVE_LOCAL, 'alias_path' => Yii::getAlias('@backend')];// 重置配置
  34. $this->uploadDrive = Yii::$app->uploadDrive->local($config);
  35. $this->filesystem = $this->uploadDrive->entity();
  36. $result = $this->filesystem->writeStream($path, $stream);
  37. if (!$result) {
  38. throw new NotFoundHttpException('文件写入失败');
  39. }
  40. if (is_resource($stream)) {
  41. fclose($stream);
  42. }
  43. } else {
  44. throw new NotFoundHttpException('上传失败,可能文件太大了');
  45. }
  46. return $this->success('上传成功', ['path' => 'backend' . $path]);
  47. } catch (\Exception $e) {
  48. return $this->error('上传失败', $e->getMessage());
  49. }
  50. }
  51. }
  52. /**
  53. * 微信js文件上传
  54. * @Author: lun
  55. * @DateTime: 2022/7/26 0026 16:33
  56. * @Copyright: copyright (c) 2021 广东七件事集团
  57. * @return mixed|void
  58. */
  59. public function actionWechatJs()
  60. {
  61. if (Yii::$app->request->isPost) {
  62. try {
  63. $file = UploadedFile::getInstanceByName($this->uploadFileName);
  64. if (!$file->getHasError()) {
  65. $h5_path = Yii::$app->basePath.'/../h5';
  66. // 文件上传地址
  67. $path_config = [
  68. [
  69. 'path' => '/web/'.$file->name,// 重置配置
  70. 'config' => ['drive' => Attachment::DRIVE_LOCAL, 'alias_path' => Yii::getAlias('@api')],// 重置配置
  71. ],
  72. [
  73. 'path' => '/web/'.$file->name,// 重置配置
  74. 'config' => ['drive' => Attachment::DRIVE_LOCAL, 'alias_path' => Yii::getAlias('@backend')],// 重置配置
  75. ],
  76. [
  77. 'path' => $file->name,// 重置配置
  78. 'config' => ['drive' => Attachment::DRIVE_LOCAL, 'alias_path' => $h5_path],// 重置配置
  79. ],
  80. ];
  81. foreach ($path_config as $item) {
  82. $stream = fopen($file->tempName, 'r+');
  83. $item['config']['disable_asserts'] = true;
  84. $this->uploadDrive = Yii::$app->uploadDrive->local($item['config']);
  85. $this->filesystem = $this->uploadDrive->entity();
  86. $config = $this->filesystem->getConfig();
  87. $config->set('disable_asserts', true);// 设置覆盖上传
  88. $result = $this->filesystem->writeStream($item['path'], $stream);
  89. if (!$result) {
  90. throw new NotFoundHttpException('文件写入失败');
  91. }
  92. if (is_resource($stream)) {
  93. fclose($stream);
  94. }
  95. }
  96. } else {
  97. throw new NotFoundHttpException('上传失败,可能文件太大了');
  98. }
  99. $this->success('上传成功', ['path' => $file->name]);
  100. } catch (\Exception $e) {
  101. return $this->error('上传失败', $e->getMessage());
  102. }
  103. }
  104. }
  105. }