FileController.php 4.7 KB

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