Uploader.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. <?php
  2. namespace upload;
  3. class Uploader
  4. {
  5. /**
  6. * @title: 所有可上传文件的mime数组
  7. * @desc: 描述
  8. * @return {*}
  9. * @author: Rock
  10. * @method: POST
  11. * @Date: 2023-05-18 14:23:37
  12. */
  13. public static $mimes = [
  14. 'xl' => 'application/excel',
  15. 'js' => 'application/javascript',
  16. 'hqx' => 'application/mac-binhex40',
  17. 'cpt' => 'application/mac-compactpro',
  18. 'bin' => 'application/macbinary',
  19. 'doc' => 'application/msword',
  20. 'word' => 'application/msword',
  21. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  22. 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  23. 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
  24. 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  25. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  26. 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  27. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  28. // 'docx' => 'application/zip',
  29. 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  30. 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
  31. 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  32. 'class' => 'application/octet-stream',
  33. 'dll' => 'application/octet-stream',
  34. 'dms' => 'application/octet-stream',
  35. 'exe' => 'application/octet-stream',
  36. 'lha' => 'application/octet-stream',
  37. 'lzh' => 'application/octet-stream',
  38. 'psd' => 'application/octet-stream',
  39. 'sea' => 'application/octet-stream',
  40. 'so' => 'application/octet-stream',
  41. 'oda' => 'application/oda',
  42. 'pdf' => 'application/pdf',
  43. 'ai' => 'application/postscript',
  44. 'eps' => 'application/postscript',
  45. 'ps' => 'application/postscript',
  46. 'smi' => 'application/smil',
  47. 'smil' => 'application/smil',
  48. 'mif' => 'application/vnd.mif',
  49. 'xls' => 'application/vnd.ms-excel',
  50. 'ppt' => 'application/vnd.ms-powerpoint',
  51. 'wbxml' => 'application/vnd.wap.wbxml',
  52. 'wmlc' => 'application/vnd.wap.wmlc',
  53. 'dcr' => 'application/x-director',
  54. 'dir' => 'application/x-director',
  55. 'dxr' => 'application/x-director',
  56. 'dvi' => 'application/x-dvi',
  57. 'gtar' => 'application/x-gtar',
  58. 'php3' => 'application/x-httpd-php',
  59. 'php4' => 'application/x-httpd-php',
  60. 'php' => 'application/x-httpd-php',
  61. 'phtml' => 'application/x-httpd-php',
  62. 'phps' => 'application/x-httpd-php-source',
  63. 'swf' => 'application/x-shockwave-flash',
  64. 'sit' => 'application/x-stuffit',
  65. 'tar' => 'application/x-tar',
  66. 'tgz' => 'application/x-tar',
  67. 'json'=> 'application/json',
  68. 'xht' => 'application/xhtml+xml',
  69. 'xhtml' => 'application/xhtml+xml',
  70. 'zip' => 'application/zip',
  71. 'mid' => 'audio/midi',
  72. 'midi' => 'audio/midi',
  73. 'mp2' => 'audio/mpeg',
  74. 'mp3' => 'audio/mpeg',
  75. 'm4a' => 'audio/mp4',
  76. 'mpga' => 'audio/mpeg',
  77. 'aif' => 'audio/x-aiff',
  78. 'aifc' => 'audio/x-aiff',
  79. 'aiff' => 'audio/x-aiff',
  80. 'ram' => 'audio/x-pn-realaudio',
  81. 'rm' => 'audio/x-pn-realaudio',
  82. 'rpm' => 'audio/x-pn-realaudio-plugin',
  83. 'ra' => 'audio/x-realaudio',
  84. 'wav' => 'audio/x-wav',
  85. 'mka' => 'audio/x-matroska',
  86. 'bmp' => 'image/bmp',
  87. 'gif' => 'image/gif',
  88. 'jpeg' => 'image/jpeg',
  89. 'jpe' => 'image/jpeg',
  90. 'jpg' => 'image/jpeg',
  91. 'png' => 'image/png',
  92. 'tiff' => 'image/tiff',
  93. 'tif' => 'image/tiff',
  94. 'webp' => 'image/webp',
  95. 'heif' => 'image/heif',
  96. 'heifs' => 'image/heif-sequence',
  97. 'heic' => 'image/heic',
  98. 'heics' => 'image/heic-sequence',
  99. 'eml' => 'message/rfc822',
  100. 'css' => 'text/css',
  101. 'html' => 'text/html',
  102. 'htm' => 'text/html',
  103. 'shtml' => 'text/html',
  104. 'log' => 'text/plain',
  105. 'text' => 'text/plain',
  106. 'txt' => 'text/plain',
  107. 'rtx' => 'text/richtext',
  108. 'rtf' => 'text/rtf',
  109. 'vcf' => 'text/vcard',
  110. 'vcard' => 'text/vcard',
  111. 'ics' => 'text/calendar',
  112. 'xml' => 'text/xml',
  113. 'xsl' => 'text/xml',
  114. 'wmv' => 'video/x-ms-wmv',
  115. 'mpeg' => 'video/mpeg',
  116. 'mpe' => 'video/mpeg',
  117. 'mpg' => 'video/mpeg',
  118. 'mp4' => 'video/mp4',
  119. 'm4v' => 'video/mp4',
  120. 'mov' => 'video/quicktime',
  121. 'qt' => 'video/quicktime',
  122. 'rv' => 'video/vnd.rn-realvideo',
  123. 'avi' => 'video/x-msvideo',
  124. 'movie' => 'video/x-sgi-movie',
  125. 'webm' => 'video/webm',
  126. 'mkv' => 'video/x-matroska',
  127. 'apk' => 'application/vnd.android.package-archive',
  128. ];
  129. /**
  130. * @title: 文件上传配置
  131. * @desc: 描述
  132. * @return {*}
  133. * @author: Rock
  134. * @method: POST
  135. * @Date: 2023-05-18 14:24:06
  136. */
  137. private $config = array(
  138. 'maxSize' => -1,// 上传文件的最大值
  139. 'supportMulti' => true,// 是否支持多文件上传
  140. 'allowExts' => array(),// 允许上传的文件后缀留空不作后缀检查
  141. 'allowTypes' => array(),// 允许上传的文件类型 留空不做检查
  142. 'autoSub' => false,// 启用子目录保存文件
  143. 'subType' => 'hash',// 子目录创建方式 可以使用hash date custom
  144. 'subDir' => '', //子目录名称 subType为custom方式后有效
  145. 'dateFormat' => 'Ymd',
  146. 'hashLevel' => 1, // hash的目录层次
  147. 'basePath' => '',// 上传文件保存路径
  148. 'autoCheck' => true, // 是否自动检查附件
  149. 'uploadReplace' => false,// 存在同名是否覆盖
  150. 'saveRule' => 'uniqid',// 上传文件命名规则,可选uniqid、md5、sha1
  151. 'hashType' => 'md5_file',// 上传文件Hash规则函数名
  152. 'Compress' => 1,//是否开启图片压缩
  153. 'attach_format_image' => 'jpg,jpeg,png,gif,bmp',
  154. 'attach_format_image_size' => '2MB',
  155. 'attach_format_video' => 'mp4,3gp,avi,mpeg',
  156. 'attach_format_video_size' => '50MB',
  157. 'attach_format_audio' => 'mp3,wav',
  158. 'attach_format_audio_size' => '5MB',
  159. 'attach_format_file' => 'xls,xlsx,txt,zip,apk',
  160. 'attach_format_file_size' => '2MB'
  161. );
  162. // 错误信息
  163. private $error = '';
  164. // 上传成功的文件信息
  165. private $uploadFileInfo ;
  166. // 获取配置
  167. public function __get($name){
  168. if(isset($this->config[$name])) {
  169. return $this->config[$name];
  170. }
  171. return null;
  172. }
  173. // 设置配置
  174. public function __set($name,$value){
  175. if(isset($this->config[$name])) {
  176. $this->config[$name] = $value;
  177. }
  178. }
  179. // 判断配置是否已设置
  180. public function __isset($name){
  181. return isset($this->config[$name]);
  182. }
  183. /**
  184. * @title: 架构函数
  185. * @desc: 当需要使用自定义的配置时,传$config参数即可
  186. * @param {array} {config} {} {自定义配置数组}
  187. * @return {*}
  188. * @author: Rock
  189. * @method: POST
  190. * @Date: 2023-05-18 14:25:18
  191. */
  192. public function __construct(array $config=[])
  193. {
  194. $this->config = array_merge($this->config,$config);
  195. // $this->saveRule = $this->config['saveRule'](time().GetRandStr(10));
  196. $this->basePath = empty($this->config['basePath'])?$this->basePath = public_path().'uploads'.DS:$this->basePath = $this->config['basePath'];
  197. }
  198. /**
  199. * 保存上传的单个文件
  200. * @access public
  201. * @param mixed $name 数据
  202. * @param string $value 数据表名
  203. * @return string
  204. */
  205. private function save($file) {
  206. $filename = $file['savepath'].$file['savename'];
  207. if(!$this->uploadReplace && is_file($filename)) {
  208. // 不覆盖同名文件
  209. $this->error = '文件已经存在!'.$filename;
  210. return false;
  211. }
  212. // 如果是图像文件 检测文件格式
  213. if( in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png','swf'))) {
  214. $info = getimagesize($file['tmp_name']);
  215. if(false === $info || ('gif' == strtolower($file['extension']) && empty($info['bits']))){
  216. $this->error = '非法图像文件';
  217. return false;
  218. }
  219. }
  220. if(!move_uploaded_file($file['tmp_name'], $this->autoCharset($filename,'utf-8','gbk'))) {
  221. $this->error = '文件上传保存错误!';
  222. return false;
  223. }
  224. return true;
  225. }
  226. /**
  227. * @title: 检查自定义目录是否可以保存文件
  228. * @desc: 描述
  229. * @param {string} {savePath} {} {文件保存位置的绝对路径}
  230. * @return {*}
  231. * @author: Rock
  232. * @method: POST
  233. * @Date: 2023-05-18 15:01:45
  234. */
  235. private function checkCustomerDir($savePath = '')
  236. {
  237. if(!is_dir($savePath)) {
  238. // 检查目录是否编码后的
  239. if(is_dir(base64_decode($savePath))) {
  240. return base64_decode($savePath);
  241. }else{
  242. $this->error = '自定义上传目录'.$savePath.'不存在';
  243. return false;
  244. }
  245. }elseif(!is_writeable($savePath)) {
  246. $this->error = '自定义上传目录'.$savePath.'不可写';
  247. return false;
  248. }else{
  249. return $savePath;
  250. }
  251. }
  252. /**
  253. * @title: 开始上传文件
  254. * @desc: 描述
  255. * @param {string} {savePath} {} {文件保存路径,不传则使用默认位置}
  256. * @return {*}
  257. * @author: Rock
  258. * @method: POST
  259. * @Date: 2023-05-18 15:32:42
  260. */
  261. public function upload(string $savePath ='') {
  262. //检查用户自定义目录
  263. if(!empty($savePath)){
  264. // 当自定义路径无法保存时,返回false,并提示错误
  265. $savePath = $this->checkCustomerDir($savePath);
  266. if(!$savePath){
  267. return false;
  268. }
  269. $customerPath = true;
  270. }else{
  271. $customerPath = false;
  272. }
  273. $fileInfo = array();//用于保存上传的文件信息
  274. $isUpload = false;//判断是否已上传
  275. // 对$_FILES数组信息处理
  276. $files = $this->dealFiles();
  277. $finfo = finfo_open(FILEINFO_MIME);
  278. foreach($files as $key => $file) {
  279. //过滤无效的上传
  280. if(!empty($file['name'])) {
  281. //登记上传文件的扩展信息
  282. if(!isset($file['key'])) $file['key'] = $key;
  283. if(empty($file['tmp_name'])){
  284. return false;
  285. }
  286. $mimeInfo = finfo_file($finfo,$file['tmp_name']);
  287. $mimeInfo = $mimeInfo?explode(';',$mimeInfo)[0]:'';
  288. // xlsx和docx文件,通过mime识别可能会被识别为zip
  289. if($file['type'] == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' && $mimeInfo == 'application/zip'){
  290. $file['mime'] = $file['type'];
  291. }elseif($file['type']=='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' && $mimeInfo == 'application/zip'){
  292. $file['mime'] = $file['type'];
  293. }else{
  294. $file['mime'] = $mimeInfo;
  295. }
  296. if($file['type']!=$mimeInfo){
  297. WLog('FileInfoType','文件类型识别错误'.$file["name"].':'.$mimeInfo);
  298. }
  299. $file['extension'] = $this->getExtByMine( $file['mime']);
  300. $file['type'] = $this->getTypeByMime($file['mime']);
  301. $file['savepath'] = $customerPath?$savePath:$this->getSaveDir($file['mime']);
  302. $file['savename'] = $this->getSaveName($file);
  303. // 自动检查附件
  304. if($this->autoCheck) {
  305. if(!$this->check($file))
  306. return false;
  307. }
  308. //保存上传文件
  309. if(!$this->save($file)) return false;
  310. if(function_exists($this->hashType)) {
  311. $fun = $this->hashType;
  312. $file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk'));
  313. }
  314. //上传成功后保存文件信息,供其他地方调用
  315. unset($file['tmp_name'],$file['error']);
  316. $fileInfo[] = $file;
  317. $isUpload = true;
  318. }
  319. }
  320. if($isUpload) {
  321. $this->uploadFileInfo = $fileInfo;
  322. $this->Compress();
  323. $this->createPreview();
  324. return true;
  325. }else{
  326. //判断是否有base64的文件上传
  327. $data = input();
  328. foreach($data as $name=>$base64File){
  329. preg_match("/^data:(.+);base64,(.+)/",$base64File,$matches);
  330. $file = [];
  331. if(count($matches)>0){
  332. $ext = $this->getExtByMine($matches[1]);
  333. $fname = time().mt_rand(00000,99999);
  334. $file['name'] = $fname;
  335. $file['key'] = $name;
  336. $file['mime'] = $matches[1];
  337. $file['extension'] = $this->getExtByMine($matches[1]);
  338. $file['type'] = $this->getTypeByMime($matches[1]);
  339. $file['savepath'] = $customerPath?$savePath:$this->getSaveDir($matches[1]);
  340. $file['savename'] = $fname.".".$ext;
  341. $f = file_put_contents($file['savepath'].$file['savename'],base64_decode($matches[2]));
  342. if(function_exists($this->hashType)) {
  343. $fun = $this->hashType;
  344. $file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk'));
  345. }
  346. $file['size'] = filesize($file['savepath']);
  347. $fileInfo[] = $file;
  348. }
  349. }
  350. if(empty($fileInfo)){
  351. $this->error = '没有选择上传文件';
  352. return false;
  353. }else{
  354. $this->uploadFileInfo = $fileInfo;
  355. $this->Compress();
  356. $this->createPreview();
  357. return true;
  358. }
  359. }
  360. }
  361. /**
  362. * @title: 对$_FILES转换上传文件数组变量为正确的方式
  363. * @desc: 描述
  364. * @param {*} $files
  365. * @return {*}
  366. * @author: Rock
  367. * @method: POST
  368. * @Date: 2023-05-18 17:32:46
  369. */
  370. private function dealFiles() {
  371. $fileArray = array();
  372. $n = 0;
  373. foreach ($_FILES as $key=>$file){
  374. if(is_array($file['name'])) {
  375. $keys = array_keys($file);
  376. $count = count($file['name']);
  377. for ($i=0; $i<$count; $i++) {
  378. $fileArray[$n]['key'] = $key;
  379. foreach ($keys as $_key){
  380. $fileArray[$n][$_key] = $file[$_key][$i];
  381. }
  382. $n++;
  383. }
  384. }else{
  385. $fileArray[$key] = $file;
  386. }
  387. }
  388. return $fileArray;
  389. }
  390. /**
  391. * @title: 获取错误代码信息
  392. * @desc: 描述
  393. * @param {int} {errorNo} {} {错误编码}
  394. * @return {*}
  395. * @author: Rock
  396. * @method: POST
  397. * @Date: 2023-05-18 17:42:19
  398. */
  399. protected function error($errorNo) {
  400. switch($errorNo) {
  401. case 1:
  402. $this->error = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
  403. break;
  404. case 2:
  405. $this->error = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
  406. break;
  407. case 3:
  408. $this->error = '文件只有部分被上传';
  409. break;
  410. case 4:
  411. $this->error = '没有文件被上传';
  412. break;
  413. case 6:
  414. $this->error = '找不到临时文件夹';
  415. break;
  416. case 7:
  417. $this->error = '文件写入失败';
  418. break;
  419. default:
  420. $this->error = '未知上传错误!';
  421. }
  422. return ;
  423. }
  424. /**
  425. * @title: 根据上传文件命名规则取得保存文件名
  426. * @desc: 描述
  427. * @param {array} {fileinfo} {} {文件信息}
  428. * @return {*}
  429. * @author: Rock
  430. * @method: POST
  431. * @Date: 2023-05-18 17:52:10
  432. */
  433. private function getSaveName(array $fileinfo)
  434. {
  435. $rule = $this->saveRule;
  436. if(empty($rule)) {//没有定义命名规则,则保持文件名不变
  437. $saveName = $fileinfo['name'];
  438. }else {
  439. if(function_exists($rule)) {
  440. //使用函数生成一个唯一文件标识号
  441. $saveName = $rule(time()).rand(1000,9999).".".$fileinfo['extension'];
  442. }else {
  443. //使用给定的文件名作为标识号
  444. $saveName = $rule.rand(1000,9999).".".$fileinfo['extension'];
  445. }
  446. }
  447. if($this->autoSub) {
  448. // 使用子目录保存文件
  449. $fileinfo['savename'] = $saveName;
  450. $saveName = $this->getSubName($fileinfo).$saveName;
  451. }
  452. return $saveName;
  453. }
  454. /**
  455. * 获取子目录的名称
  456. * @access private
  457. * @param array $file 上传的文件信息
  458. * @return string
  459. */
  460. private function getSubName($file) {
  461. switch($this->subType) {
  462. case 'custom':
  463. $dir = $this->subDir;
  464. break;
  465. case 'date':
  466. $dir = date($this->dateFormat,time()).DS;
  467. break;
  468. case 'hash':
  469. default:
  470. $name = md5($file['savename']);
  471. $dir = '';
  472. for($i=0;$i<$this->hashLevel;$i++) {
  473. $dir .= $name[$i].DS;
  474. }
  475. break;
  476. }
  477. if(!is_dir($file['savepath'].$dir)) {
  478. mkdir($file['savepath'].$dir,0777,true);
  479. }
  480. return $dir;
  481. }
  482. /**
  483. * 检查上传的文件
  484. * @access private
  485. * @param array $file 文件信息
  486. * @return boolean
  487. */
  488. private function check($file):bool
  489. {
  490. if($file['error']!== 0) {
  491. //文件上传失败
  492. //捕获错误代码
  493. $this->error($file['error']);
  494. return false;
  495. }
  496. //文件上传成功,进行自定义规则检查
  497. //检查文件大小
  498. if(!$this->checkSize($file['size'])) {
  499. $this->error = '上传文件大小不符!';
  500. return false;
  501. }
  502. //检查文件Mime类型
  503. if(!$this->checkType($file['type'])) {
  504. $this->error = '上传文件MIME类型不允许!';
  505. return false;
  506. }
  507. //检查文件类型
  508. if(!$this->checkExt($file['extension'])) {
  509. $this->error ='上传文件类型'.$file['extension'].'不允许';
  510. return false;
  511. }
  512. //检查是否合法上传
  513. if(!$this->checkUpload($file['tmp_name'])) {
  514. $this->error = '非法上传文件!';
  515. return false;
  516. }
  517. return true;
  518. }
  519. // 自动转换字符集 支持数组转换
  520. private function autoCharset($fContents, $from='gbk', $to='utf-8') {
  521. $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
  522. $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
  523. if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
  524. //如果编码相同或者非字符串标量则不转换
  525. return $fContents;
  526. }
  527. if (function_exists('mb_convert_encoding')) {
  528. return mb_convert_encoding($fContents, $to, $from);
  529. } elseif (function_exists('iconv')) {
  530. return iconv($from, $to, $fContents);
  531. } else {
  532. return $fContents;
  533. }
  534. }
  535. /**
  536. * 检查上传的文件类型是否合法
  537. * @access private
  538. * @param string $type 数据
  539. * @return boolean
  540. */
  541. private function checkType($type) {
  542. if(!empty($this->allowTypes))
  543. return in_array(strtolower($type),$this->allowTypes);
  544. return true;
  545. }
  546. /**
  547. * 检查上传的文件后缀是否合法
  548. * @access private
  549. * @param string $ext 后缀名
  550. * @return boolean
  551. */
  552. private function checkExt($ext) {
  553. if(!empty($this->allowExts))
  554. return in_array(strtolower($ext),$this->allowExts,true);
  555. return true;
  556. }
  557. /**
  558. * 检查文件大小是否合法
  559. * @access private
  560. * @param integer $size 数据
  561. * @return boolean
  562. */
  563. private function checkSize($size) {
  564. return !($size > $this->maxSize) || (-1 == $this->maxSize);
  565. }
  566. /**
  567. * 检查文件是否非法提交
  568. * @access private
  569. * @param string $filename 文件名
  570. * @return boolean
  571. */
  572. private function checkUpload($filename) {
  573. return is_uploaded_file($filename);
  574. }
  575. /**
  576. * 取得上传文件的后缀
  577. * @access private
  578. * @param string $filename 文件名
  579. * @return boolean
  580. */
  581. private function getExt($filename) {
  582. $pathinfo = pathinfo($filename);
  583. return $pathinfo['extension'];
  584. }
  585. /**
  586. * 取得上传文件的信息
  587. * @access public
  588. * @return array
  589. */
  590. public function getUploadFileInfo() {
  591. return $this->uploadFileInfo;
  592. }
  593. /**
  594. * 取得最后一次错误信息
  595. * @access public
  596. * @return string
  597. */
  598. public function getErrorMsg() {
  599. return $this->error;
  600. }
  601. /**根据mine类型获取文件后缀 */
  602. public function getExtByMine($mime=""){
  603. $mimes = self::$mimes;
  604. $mime = strtolower($mime);
  605. $mimes = array_flip($mimes);
  606. if (array_key_exists($mime, $mimes)) {
  607. return $mimes[$mime];
  608. }
  609. return '';
  610. }
  611. /**获取文件类型获取应该存放的文件夹 */
  612. public function getSaveDir($mime)
  613. {
  614. $dir = $this->getTypeByMime($mime);
  615. $savePath = $this->basePath.$dir.DS.date('Ymd',time()).DS;
  616. if(!is_dir($savePath) || !is_writeable($savePath)){
  617. mkdir($savePath,0777,true);
  618. }
  619. return $savePath;
  620. }
  621. /**根据mime获取文件分类 */
  622. public function getTypeByMime($mime)
  623. {
  624. $dirlist=[
  625. 'image' =>'images',
  626. 'face' =>'facepic',
  627. 'file' =>'files',
  628. 'audio' =>'audio',
  629. 'video' =>'video'
  630. ];
  631. if(empty($mime))return 'other';
  632. $type = explode('/',$mime);
  633. $dir = isset($dirlist[$type[0]])?$dirlist[$type[0]]:'other';
  634. return $dir;
  635. }
  636. /**按文件类型设置上传限制 */
  637. public function setLimit($ext)
  638. {
  639. if(empty($ext))return 'other';
  640. $mime = isset(self::$mimes[$ext])?self::$mimes[$ext]:'';
  641. if(empty($mime))return 'other';
  642. $type = explode('/',$mime);
  643. if($type=='image' || $type=='face'){
  644. //读取图片类型的设置
  645. $this->allowExts=explode(',',$this->config['attach_format_image']);
  646. $this->maxSize=GetSizeToBit($this->config['attach_format_image_size']);
  647. }else if($type=='video'){
  648. //读取视频类型的设置
  649. $this->allowExts=explode(',',$this->config['attach_format_video']);
  650. $this->maxSize=GetSizeToBit($this->config['attach_format_video_size']);
  651. }else if($type=='audio'){
  652. //读取音频类型的设置
  653. $this->allowExts=explode(',',$this->config['attach_format_audio']);
  654. $this->maxSize=GetSizeToBit($this->config['attach_format_audio_size']);
  655. }else{
  656. //读取其它类型的设置
  657. $this->allowExts=explode(',',$this->config['attach_format_file']);
  658. $this->maxSize=GetSizeToBit($this->config['attach_format_file_size']);
  659. }
  660. }
  661. /**压缩文件 */
  662. public function Compress()
  663. {
  664. if($this->config['Compress']==1){
  665. foreach($this->uploadFileInfo as &$file){
  666. if('jpeg'==strtolower($file['extension'])){
  667. $file['extension'] = 'jpg';
  668. }
  669. if($file['type']=='images'){
  670. $result=\image\Image::convert($file['savepath'].$file['savename'],'',$file['extension'],true);
  671. \image\Image::ImageCompress($result);
  672. }
  673. }
  674. }
  675. }
  676. /**
  677. * @title: 生成图片文件的预览图
  678. * @desc: 生成预览图文件的规则为原文件名加上.prev加后缀,如1234.jpg,生成的预览图文件名为1234.jpg.prev.jpg
  679. * @param {string} {file} {} {原文件绝对路径}
  680. * @param {string} {size} {80} {预览图最大尺寸(宽度或高度,限制最大的一个),单位像素}
  681. * @return {*}
  682. * @author: Rock
  683. * @method: POST
  684. * @Date: 2023-06-08 10:43:14
  685. */
  686. public function createPreview(int $size=80)
  687. {
  688. foreach($this->uploadFileInfo as &$file){
  689. if($file['type']=='images'){
  690. $url=$file['savepath'].$file['savename'];
  691. WLog('createPreview',$url);
  692. \image\Image::ImageCompress($url,$url.'.prev.jpg',$size,50);
  693. }
  694. }
  695. }
  696. }