You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

243 lines
6.2 KiB

  1. <?php
  2. namespace Kuxin\Helper;
  3. /**
  4. * Class Upload
  5. *
  6. * @package Kuxin\Helper
  7. * @author Pakey <pakey@qq.com>
  8. */
  9. class Upload
  10. {
  11. /**
  12. * @var \Kuxin\Storage
  13. */
  14. public $storage = null;
  15. //$_FILES的文件信息
  16. public $fileinfo;
  17. //自定义文件名
  18. public $fileName;
  19. //自定义文件存放目录
  20. public $fileDir;
  21. //自定义文件存放完整路径
  22. public $filePath;
  23. //自定义允许文件后缀
  24. public $allowType = "jpg|png|gif|txt|bmp|ico|doc|xls|jpeg|zip|rar";
  25. //自定义允许文件mime
  26. public $allowMime = [];
  27. //自定义文件大小 Kb
  28. public $allowMaxSize = 2048;
  29. public function __construct($storage)
  30. {
  31. $this->storage = $storage;
  32. }
  33. /**
  34. * 临时文件
  35. *
  36. * @param $fileinfo
  37. */
  38. public function setFile($fileinfo)
  39. {
  40. $this->fileinfo = $fileinfo;
  41. }
  42. /**
  43. * 设置上传的文件名
  44. *
  45. * @param $filename
  46. */
  47. public function setName($filename)
  48. {
  49. $this->fileName = $filename;
  50. }
  51. /**
  52. * 设置上传的文件路径
  53. *
  54. * @param $filedir
  55. */
  56. public function setDir($filedir)
  57. {
  58. $this->fileDir = $filedir;
  59. }
  60. /**
  61. * 设置上传的文件后缀
  62. *
  63. * @param $filetype
  64. */
  65. public function setType($filetype)
  66. {
  67. $this->allowType = $filetype;
  68. }
  69. /**
  70. * 设置上传的文件大小
  71. *
  72. * @param $filesize
  73. */
  74. public function setSize($filesize)
  75. {
  76. $this->allowMaxSize = $filesize;
  77. }
  78. /**
  79. *检测文件大小
  80. */
  81. private function checkSize()
  82. {
  83. return $this->fileinfo['size'] > 0 && ($this->fileinfo['size'] <= $this->allowMaxSize * 1024);
  84. }
  85. /**
  86. *检测文件后缀
  87. */
  88. private function checkType()
  89. {
  90. return in_array($this->getType(), explode("|", strtolower($this->allowType)));
  91. }
  92. /**
  93. *获取文件后缀
  94. */
  95. private function getType()
  96. {
  97. return strtolower(pathinfo($this->fileinfo['name'], PATHINFO_EXTENSION));
  98. }
  99. /**
  100. *获取文件完整路径
  101. */
  102. private function getPath()
  103. {
  104. if (empty($this->fileDir))
  105. $this->fileDir = date('Ym') . '/' . date('d');
  106. if (!$this->fileName)
  107. $this->fileName = md5($this->fileinfo['name'] . $this->fileinfo['size']);
  108. $this->filePath = $this->fileDir . '/' . $this->fileName . "." . $this->getType();
  109. }
  110. /**
  111. * 检测mime类型
  112. *
  113. * @return bool
  114. */
  115. protected function checkMime()
  116. {
  117. return !(!empty($this->allowMime) && !in_array($this->fileinfo['type'], $this->allowMime));
  118. }
  119. /**
  120. * 错误返回
  121. *
  122. * @param $info
  123. * @return array
  124. */
  125. private function error($info)
  126. {
  127. return ['status' => 0, 'info' => $info];
  128. }
  129. /**
  130. *上传文件
  131. */
  132. public function save()
  133. {
  134. if ($this->fileinfo['error'] !== 0) {
  135. $this->error($this->geterrorinfo($this->fileinfo['error']));
  136. }
  137. //检测文件大小
  138. if (!$this->checkSize()) {
  139. return $this->error("上传附件不得超过" . $this->allowMaxSize . "KB");
  140. }
  141. //校验mime信息
  142. if (!$this->checkMime()) {
  143. return $this->error("上传文件MIME类型不允许!");
  144. }
  145. //不符则警告
  146. if (!$this->checkType()) {
  147. return $this->error("正确的扩展名必须为" . $this->allowType . "其中的一种!");
  148. }
  149. //检查是否合法上传
  150. if (!is_uploaded_file($this->fileinfo['tmp_name'])) {
  151. return $this->error("非法上传文件!");
  152. }
  153. // 获取上传文件的保存信息
  154. $this->getpath();
  155. if ($this->write(file_get_contents($this->fileinfo['tmp_name']))) {
  156. $info['ext'] = $this->getType();
  157. $info['fileurl'] = $this->storage->getUrl($this->filePath);
  158. $info['filepath'] = $this->filePath;
  159. $info['filename'] = $this->fileinfo['name'];
  160. $info['hash'] = md5_file($this->fileinfo['tmp_name']);
  161. $info['size'] = $this->fileinfo['size'];
  162. return ['status' => 1, 'info' => $info];
  163. } else {
  164. return $this->error("上传失败!");
  165. }
  166. }
  167. protected function getErrorInfo($num)
  168. {
  169. switch ($num) {
  170. case 1:
  171. return '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
  172. case 2:
  173. return '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
  174. case 3:
  175. return '文件只有部分被上传';
  176. case 4:
  177. return '没有文件被上传';
  178. case 6:
  179. return '找不到临时文件夹';
  180. case 7:
  181. return '文件写入失败';
  182. default:
  183. return '未知上传错误!';
  184. }
  185. }
  186. public function write($content)
  187. {
  188. // 上传操作
  189. if (in_array($this->getType(), ['gif', 'jpg', 'jpeg', 'bmp', 'png'])) {
  190. //$imginfo = getimagesize($this->fileinfo['tmp_name']);
  191. $img = new image($this->fileinfo['tmp_name']);
  192. $content = $img->save();
  193. }
  194. return $this->storage->write($this->filePath, $content);
  195. }
  196. public function saveFromUrl($url, $content = '')
  197. {
  198. $this->fileName = $this->filePath = '';
  199. if ($content == '') {
  200. $content = http::get($url);
  201. }
  202. $this->fileinfo = [
  203. 'name' => basename(parse_url($url, PHP_URL_PATH)),
  204. 'size' => strlen($content),
  205. 'tmp_name' => $url,
  206. ];
  207. $this->getpath();
  208. if ($this->write($content)) {
  209. $info['ext'] = $this->getType();
  210. $info['fileurl'] = $this->storage->getUrl($this->filePath);
  211. $info['filepath'] = $this->filePath;
  212. $info['filename'] = $this->fileinfo['name'];
  213. $info['hash'] = md5($content);
  214. $info['size'] = $this->fileinfo['size'];
  215. return ['status' => 1, 'info' => $info];
  216. } else {
  217. return $this->error("上传失败!");
  218. }
  219. }
  220. }