UploadService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace service;
  12. class UploadService
  13. {
  14. private static $uploadStatus;
  15. //上传图片的大小 2MB 单位字节
  16. private static $imageValidate = ['size' => 2097152, 'ext' => 'jpg,jpeg,png,gif', 'mime' => 'image/jpeg,image/gif,image/png'];
  17. private static $fileExt = ['pem', 'mp3', 'wma', 'wav', 'amr', 'mp4', 'key'];//上传文件后缀类型
  18. private static $fileMime = ['text/plain', 'audio/mpeg','application/x-x509-ca-cert','application/octet-stream']; //上传文件类型
  19. /**
  20. * 初始化
  21. */
  22. private static function init()
  23. {
  24. self::$uploadStatus = new \StdClass();
  25. }
  26. /**
  27. * 返回失败信息
  28. * @param $error
  29. * @return mixed
  30. */
  31. protected static function setError($error)
  32. {
  33. self::$uploadStatus->status = false;
  34. self::$uploadStatus->error = $error;
  35. return self::$uploadStatus;
  36. }
  37. /**
  38. * 返回成功信息
  39. * @param $path
  40. * @param \think\File $fileInfo
  41. * @return mixed
  42. */
  43. protected static function successful($path, \think\File $fileInfo)
  44. {
  45. $filePath = DS . $path . DS . $fileInfo->getSaveName();
  46. self::$uploadStatus->filePath = self::pathToUrl($filePath);
  47. self::$uploadStatus->fileInfo = $fileInfo;
  48. self::$uploadStatus->uploadPath = $path;
  49. self::$uploadStatus->dir = $filePath;
  50. self::$uploadStatus->status = true;
  51. return self::$uploadStatus;
  52. }
  53. /**
  54. * 检查上传目录不存在则生成
  55. * @param $dir
  56. * @return bool
  57. */
  58. protected static function validDir($dir)
  59. {
  60. return is_dir($dir) == true || mkdir($dir, 0777, true) == true;
  61. }
  62. /**
  63. * 开启/关闭上出文件验证
  64. * @param bool $bool
  65. */
  66. protected static function autoValidate($bool = false)
  67. {
  68. self::$autoValidate = $bool;
  69. }
  70. /**
  71. * 生成上传文件目录
  72. * @param $path
  73. * @param null $root
  74. * @return string
  75. */
  76. protected static function uploadDir($path, $root = null)
  77. {
  78. if ($root === null) $root = 'uploads';
  79. return $root . DS . $path;
  80. }
  81. /**
  82. * 单图上传
  83. * @param string $fileName 上传文件名
  84. * @param string $path 上传路径
  85. * @param bool $moveName 生成文件名
  86. * @param bool $autoValidate 是否开启文件验证
  87. * @param null $root 上传根目录路径
  88. * @param string $rule 文件名自动生成规则
  89. * @return mixed
  90. */
  91. public static function image($fileName, $path, $moveName = true, $autoValidate = true, $root = null, $rule = 'uniqid')
  92. {
  93. self::init();
  94. $path = self::uploadDir($path, $root);
  95. $dir = ROOT_PATH.'public/'. $path;
  96. if (!self::validDir($dir)) return self::setError('生成上传目录失败,请检查权限!');
  97. if (!isset($_FILES[$fileName])) return self::setError('上传文件不存在!');
  98. $file = request()->file($fileName);
  99. if ($autoValidate) $file = $file->validate(self::$imageValidate);
  100. $fileInfo = $file->rule($rule)->move($dir, $moveName);
  101. if (false === $fileInfo) return self::setError($file->getError());
  102. return self::successful($path, $fileInfo);
  103. }
  104. /**
  105. * 文件上传
  106. * @param string $fileName 上传文件名
  107. * @param string $path 上传路径
  108. * @param bool $moveName 生成文件名
  109. * @param bool $autoValidate 验证规则 [size:1024,ext:[],type:[]]
  110. * @param null $root 上传根目录路径
  111. * @param string $rule 文件名自动生成规则
  112. * @return mixed
  113. */
  114. public static function file($fileName, $path, $moveName = true, $autoValidate = [], $root = null, $rule = 'uniqid')
  115. {
  116. self::init();
  117. $path = self::uploadDir($path, $root);
  118. $dir = ROOT_PATH . DS . 'public' . DS . $path;
  119. if (!self::validDir($dir)) return self::setError('生成上传目录失败,请检查权限!');
  120. if (!isset($_FILES[$fileName])) return self::setError('上传文件不存在!');
  121. $uploaded_name = $_FILES[$fileName]['name'];
  122. $uploaded_ext = substr( $uploaded_name, strrpos( $uploaded_name, '.' ) + 1);
  123. $uploaded_type = $_FILES[$fileName]['type'];
  124. $uploaded_size = $_FILES[$fileName]['size'];
  125. $extension = strtolower(pathinfo($uploaded_name, PATHINFO_EXTENSION));
  126. if (strtolower($extension) === 'php' || !$extension) return self::setError('上传文件非法!');
  127. if(in_array(strtolower($uploaded_ext),self::$fileExt) && ($uploaded_size < 2097152) && in_array($uploaded_type,self::$fileMime)) {
  128. $file = request()->file($fileName);
  129. if (count($autoValidate) > 0) $file = $file->validate($autoValidate);
  130. $fileInfo = $file->rule($rule)->move($dir, $moveName);
  131. if (false === $fileInfo) return self::setError($file->getError());
  132. return self::successful($path, $fileInfo);
  133. }else{
  134. //无效文件
  135. return self::setError('上传文件非法或文件太大!');
  136. }
  137. }
  138. public static function pathToUrl($path)
  139. {
  140. return trim(str_replace(DS, '/', $path), '.');
  141. }
  142. public static function openImage($filePath)
  143. {
  144. return \think\Image::open($filePath);
  145. }
  146. /**
  147. * 图片压缩
  148. *
  149. * @param string $filePath 文件路径
  150. * @param int $ratio 缩放比例 1-9
  151. * @param string $pre 前缀
  152. * @return string 压缩图片路径
  153. */
  154. public static function thumb($filePath, $ratio = 8, $pre = 's_')
  155. {
  156. $filePath = '.' . ltrim($filePath, '.');
  157. $img = self::openImage($filePath);
  158. $width = $img->width() * $ratio / 10;
  159. $height = $img->height() * $ratio / 10;
  160. $dir = dirname($filePath);
  161. $fileName = basename($filePath);
  162. $savePath = $dir . DS . $pre . $fileName;
  163. $img->thumb($width, $height)->save($savePath);
  164. return ltrim($savePath, '.');
  165. }
  166. }