Upload.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * 文件上传
  4. * @聊天过程中的文件上传(图片、声音、视频等)
  5. */
  6. namespace app\home\controller;
  7. use think\File;
  8. class Upload extends AdminControl
  9. {
  10. /**
  11. * 上传文件目录
  12. * @access protected
  13. * @var string
  14. */
  15. protected $upload_dir;
  16. /**
  17. * domain
  18. * @access public
  19. * @var string
  20. */
  21. protected $domain;
  22. /**
  23. * 文件类型
  24. * @access protected
  25. * @var array
  26. */
  27. protected $file_type = [
  28. 'image' => [
  29. 'title' => '图片',
  30. 'dir' => '/image/'
  31. ],
  32. 'video' => [
  33. 'title' => '视频',
  34. 'dir' => '/video/'
  35. ],
  36. 'voice' => [
  37. 'title' => '声音',
  38. 'dir' => '/voice/'
  39. ],
  40. 'file' => [
  41. 'title' => '文件',
  42. 'dir' => '/file/'
  43. ],
  44. ];
  45. /**
  46. * 文件允许大小
  47. * @access proetcted
  48. * @var array
  49. */
  50. protected $file_allow_size = [
  51. 'image' => 5120000,//5M
  52. 'video' => 20480000,//20M
  53. 'voice' => 20480000,//20M
  54. 'file' => 20480000,//20M
  55. ];
  56. /**
  57. * 文件允许格式
  58. * @access protected
  59. * @var array
  60. */
  61. protected $file_allow_ext = [
  62. 'image' => [
  63. 'gif', 'jpg', 'jpeg', 'png', 'bmp'
  64. ],
  65. 'video' => [
  66. 'mp4', 'avi', 'ogg', 'flv', 'webm',
  67. 'ogv', 'mpg'
  68. ],
  69. 'voice' => [
  70. 'mp3', 'ogg', 'wav', 'm4a'
  71. ],
  72. 'file' => [],
  73. ];
  74. public function _initialize()
  75. {
  76. parent::_initialize();
  77. if (!empty(config('FILE_UPLOADS_DIR'))) {
  78. $this->upload_dir = config('FILE_UPLOADS_DIR');
  79. }
  80. $this->domain = !empty($this->request->domain()) ? $this->request->domain() : '';
  81. }
  82. /**
  83. * @上传文件
  84. * @param
  85. * @access
  86. * @return
  87. */
  88. public function upFile()
  89. {
  90. $type = !empty($this->request->param('type')) ? $this->request->param('type') : '';
  91. if (empty($type)) {
  92. return ['status' => 0, 'msg' => 'error[expect type]'];
  93. }
  94. if (!in_array($type, array_keys($this->file_type))) {
  95. return ['status' => 0, 'msg' => 'error[type not exist]'];
  96. }
  97. try {
  98. $file = $this->request->file($type);
  99. } catch (\Exception $e) {
  100. return ['status' => 0, 'msg' => 'error[file empty]'];
  101. }
  102. if (empty($file)) {
  103. return ['status' => 0, 'msg' => 'error[file empty]'];
  104. }
  105. $res = $this->_upFile($file, $type);
  106. if ($res['status'] != 1) {
  107. return $res;
  108. }
  109. return ['status' => 200, 'msg' => 'success', 'data' => $res['data']];
  110. }
  111. /**
  112. * @上传操作
  113. * @param object $file
  114. * @param string $type
  115. * @access protected
  116. * @return array
  117. */
  118. protected function _upFile(File $file, $type)
  119. {
  120. //验证文件类型
  121. if (!empty($this->file_allow_ext[$type]) && $file->checkExt($this->file_allow_ext[$type]) === false) {
  122. return [
  123. 'status' => 0,
  124. 'msg' => '文件格式不允许',
  125. ];
  126. }
  127. //验证大小
  128. if (!empty($this->file_allow_size[$type]) && $file->checkSize($this->file_allow_size[$type]) === false) {
  129. return [
  130. 'status' => 0,
  131. 'msg' => '文件大小不符',
  132. ];
  133. }
  134. $info = $file->move('.'.$this->upload_dir.$this->file_type[$type]['dir']);
  135. if (empty($info)) {
  136. return [
  137. 'status' => 0,
  138. 'msg' => $file->getError(),
  139. ];
  140. }
  141. return [
  142. 'status' => 1,
  143. 'msg' => 'success',
  144. 'data' => [
  145. 'ext' => $info->getExtension(),
  146. 'name' => $info->getFilename(),
  147. 'path' => $this->domain.str_replace("\\", "/", $this->upload_dir.$this->file_type[$type]['dir'].$info->getSaveName()),
  148. ],
  149. ];
  150. }
  151. }