Upload.php 4.2 KB

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