Core.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. <?php
  2. namespace Upload\Lib\Store;
  3. use Dever;
  4. use Dever\String\Helper as Helper;
  5. use Dever\Loader\Config;
  6. use Dever\Support\Img;
  7. class Core
  8. {
  9. protected $data;
  10. protected $config;
  11. protected $handle;
  12. protected $img;
  13. protected $output;
  14. protected $limit;
  15. protected $name;
  16. protected $path;
  17. protected $ext = '';
  18. protected $file;
  19. protected $size;
  20. protected $base = '';
  21. protected $base64 = false;
  22. protected $yun = false;
  23. protected $id = false;
  24. /**
  25. * __construct
  26. *
  27. * @return mixed
  28. */
  29. public function __construct($config, $data = array())
  30. {
  31. $this->data = $data;
  32. $this->config = $config;
  33. }
  34. public function getData()
  35. {
  36. return $this->data;
  37. }
  38. /**
  39. * 获取根目录
  40. *
  41. * @return mixed
  42. */
  43. protected function root()
  44. {
  45. if (!$this->base) {
  46. $path = Config::data();
  47. $this->base = Dever::path($path . 'upload/');
  48. }
  49. return $this->base;
  50. }
  51. /**
  52. * 验证数据
  53. *
  54. * @return mixed
  55. */
  56. private function check($param)
  57. {
  58. foreach ($param as $k => $v) {
  59. $method = 'check_' . $v;
  60. $this->$method();
  61. if ($this->output['status'] == -1) {
  62. break;
  63. }
  64. }
  65. }
  66. /**
  67. * 验证key是否包含有后续处理的方法
  68. *
  69. * @return mixed
  70. */
  71. private function check_handle()
  72. {
  73. if (isset($this->data['key']) && strpos($this->data['key'], '_') !== false) {
  74. $temp = explode('_', $this->data['key']);
  75. $this->data['key'] = $temp[0];
  76. unset($temp[0]);
  77. foreach ($temp as $k => $v) {
  78. $this->handle[] = explode('=', $v);
  79. }
  80. }
  81. }
  82. /**
  83. * 验证文件是否存在
  84. *
  85. * @return mixed
  86. */
  87. private function check_file()
  88. {
  89. if ($this->data['file']['tmp_name'] == '') {
  90. $this->output['status'] = -1;
  91. $this->output['message'] = '没有选择文件';
  92. }
  93. }
  94. /**
  95. * 验证基本配置
  96. *
  97. * @return mixed
  98. */
  99. private function check_key()
  100. {
  101. if (trim($this->data['key']) == '') {
  102. $this->output['status'] = -1;
  103. $this->output['message'] = '请添加配置key';
  104. }
  105. /*
  106. else {
  107. $this->config = Dever::load('upload/upload-one', $this->data['key']);
  108. if (!$this->config) {
  109. $this->output['status'] = -1;
  110. $this->output['message'] = '此配置不存在';
  111. }
  112. }
  113. */
  114. }
  115. /**
  116. * 验证文件类型
  117. *
  118. * @return mixed
  119. */
  120. private function check_type()
  121. {
  122. if (!$this->ext) {
  123. $ext = $this->getExt($this->data['file']['tmp_name']);
  124. if (strpos($this->config['type'], $ext) === false) {
  125. $this->output['status'] = -1;
  126. $this->output['message'] = '文件格式不符合要求';
  127. }
  128. $this->ext = '.' . $ext;
  129. }
  130. }
  131. /**
  132. * 验证文件大小
  133. *
  134. * @return mixed
  135. */
  136. private function check_size()
  137. {
  138. if (!$this->limit && !$this->base64 && ($this->ext == '.jpg' || $this->ext == '.gif' || $this->ext == '.png')) {
  139. $this->limit = getimagesize($this->data['file']['tmp_name']);
  140. }
  141. # 默认30M
  142. $size = $this->config['size'] > 0 ? 1024*1024*$this->config['size'] : 30*1024*1024;
  143. if (isset($this->data['file']['size'])) {
  144. $this->size = $this->data['file']['size'];
  145. if (($size < $this->data['file']['size']) && $size > 0) {
  146. $this->output['status'] = -1;
  147. $this->output['message'] = '文件不能超过'.(($size/1024)/1024).'MB';
  148. } elseif ($this->limit && $this->config['width'] > 0 && $this->config['width'] < $this->limit[0]) {
  149. $this->output['status'] = -1;
  150. $this->output['message'] = '图片宽度不能超过' . $this->config['width'] . 'px';
  151. } elseif ($this->limit && $this->config['height'] > 0 && $this->config['height'] < $this->limit[1]) {
  152. $this->output['status'] = -1;
  153. $this->output['message'] = '图片高度不能超过' . $this->config['height'] . 'px';
  154. }
  155. $this->size = number_format($this->size/1024, 2);
  156. }
  157. }
  158. /**
  159. * 上传操作
  160. *
  161. * @return mixed
  162. */
  163. public function copy()
  164. {
  165. $this->output['status'] = 1;
  166. if (is_string($this->data['file'])) {
  167. if (strstr($this->data['file'], 'base64,')) {
  168. $temp = explode('base64,', $this->data['file']);
  169. $type = str_replace(array('data:', ';'), '', $temp[0]);
  170. $file = str_replace(' ', '+', $temp[1]);
  171. $file = str_replace('=', '', $temp[1]);
  172. $file = base64_decode($file);
  173. if (isset($this->data['pic'])) {
  174. $name = $this->data['local'] = Dever::local($this->data['pic']);
  175. } else {
  176. $name = Dever::rand(10);
  177. }
  178. $size = strlen($file);
  179. //$size = number_format(($size - ($size/8)*2)/1024, 2);
  180. $size = round(($size - ($size/8)*2)/1024, 2);
  181. $this->base64 = true;
  182. } else {
  183. $name = urldecode($this->data['file']);
  184. }
  185. $this->data['file'] = array();
  186. if (((isset($this->data['cropper']) && !$this->yun) || !isset($this->data['cropper'])) && strstr($name, 'http')) {
  187. $this->base64 = false;
  188. $this->root();
  189. header('Content-type: text/json; charset=utf-8');
  190. $path = Dever::path($this->base, 'tmp/');
  191. $ext = $name;
  192. if (strstr($name, '?')) {
  193. $temp = explode('?', $ext);
  194. $ext = $temp[0];
  195. }
  196. $this->ext = '.' . pathinfo($ext, PATHINFO_EXTENSION);
  197. $this->data['file']['name'] = 'Tmp' . sha1($name);
  198. $this->data['file']['tmp_name'] = $path . $this->data['file']['name'];
  199. if (!is_file($this->data['file']['tmp_name'])) {
  200. if (strstr($name, 'tp=webp')) {
  201. $name = str_replace('tp=webp', 'tp=jpeg', $name);
  202. } elseif (strstr($name, '.webp')) {
  203. $name = str_replace('.webp', '.jpg', $name);
  204. }
  205. if (!isset($file)) {
  206. $file = Dever::curl($name);
  207. }
  208. if (stristr($file, 'webp')) {
  209. # 将webp图片转成jpg
  210. $this->ext = '.jpg';
  211. }
  212. if (!isset($file)) {
  213. Dever::alert('错误的文件来源');
  214. }
  215. file_put_contents($this->data['file']['tmp_name'], $file);
  216. }
  217. $this->data['file']['size'] = filesize($this->data['file']['tmp_name']);
  218. } elseif (isset($file)) {
  219. $this->data['file']['name'] = $name;
  220. $this->data['file']['tmp_name'] = $file;
  221. } else {
  222. $this->data['file']['name'] = $name;
  223. $this->data['file']['tmp_name'] = $name;
  224. }
  225. if (isset($type)) {
  226. $this->data['file']['type'] = $type;
  227. }
  228. if (isset($size)) {
  229. $this->data['file']['size'] = $size;
  230. }
  231. if (isset($this->data['param'])) {
  232. $this->limit = array($this->data['param']['param_w'], $this->data['param']['param_h']);
  233. if (isset($this->data['name'])) {
  234. $param = implode('_', array_values($this->data['param']));
  235. $this->data['file']['name'] = '.' . $this->data['name'] . $param;
  236. if ($this->base64) {
  237. $this->data['name'] = $this->data['file']['name'];
  238. } else {
  239. $filename = md5($this->data['pic']) . '.' . pathinfo($this->data['pic'], PATHINFO_EXTENSION);
  240. $this->path();
  241. $file = $this->base . $this->data['key'] . $this->path . $filename;
  242. if (!is_file($file)) {
  243. $content = Dever::curl($name);
  244. file_put_contents($file, $content);
  245. }
  246. $this->data['name'] = $filename . $this->data['file']['name'];
  247. }
  248. $this->ext = '.jpg';
  249. }
  250. }
  251. } else {
  252. header("Content-type: application/json; charset=utf-8");
  253. }
  254. $this->check(array('handle', 'file', 'key', 'type', 'size'));
  255. if ($this->output['status'] == -1) {
  256. $this->delete();
  257. return $this->output;
  258. }
  259. $this->save();
  260. if ($this->output['status'] == -1) {
  261. return $this->output;
  262. }
  263. if (isset($this->handle) && is_array($this->handle)) {
  264. foreach ($this->handle as $k => $v) {
  265. $method = 'handle_' . $v[0];
  266. if (method_exists($this, $method)) {
  267. $this->$method($v);
  268. }
  269. $this->$method($v[1]);
  270. }
  271. } elseif (isset($this->config['alter']) && $this->config['alter']) {
  272. parse_str($this->config['alter'], $handle);
  273. if ($handle) {
  274. foreach ($handle as $k => $v) {
  275. $method = 'handle_' . $k;
  276. if (method_exists($this, $method)) {
  277. $this->$method($v);
  278. }
  279. }
  280. }
  281. }
  282. $this->output['uploaded'] = true;
  283. $this->output['status'] = 1;
  284. $this->output['name'] = $this->data['file']['name'];
  285. $this->output['fid'] = $this->id;
  286. if ($this->limit) {
  287. $this->output['width'] = $this->limit[0];
  288. $this->output['height'] = $this->limit[1];
  289. }
  290. if ($this->size) {
  291. $this->output['size'] = $this->size;
  292. }
  293. return $this->output;
  294. }
  295. protected function update($id)
  296. {
  297. $param['set_name'] = $this->name;
  298. $param['set_source_name'] = $this->data['file']['name'];
  299. $param['set_file'] = $this->file;
  300. $param['set_key'] = md5($this->output['url']);
  301. $param['set_ext'] = $this->ext;
  302. if (isset($this->data['cate'])) {
  303. $param['set_cate'] = $this->data['cate'];
  304. }
  305. if ($this->limit) {
  306. $param['set_width'] = $this->limit[0];
  307. $param['set_height'] = $this->limit[1];
  308. }
  309. if ($this->size) {
  310. $param['set_size'] = $this->size;
  311. }
  312. if (isset($this->data['search']) && $this->data['search']) {
  313. $param['search'] = $this->data['search'];
  314. }
  315. $this->id = $param['where_id'] = $id;
  316. Dever::db('upload/file')->update($param);
  317. }
  318. protected function insert()
  319. {
  320. $param['add_name'] = $this->name;
  321. $param['add_source_name'] = $this->data['file']['name'];
  322. $param['add_file'] = $this->file;
  323. $param['add_key'] = md5($this->output['url']);
  324. $param['add_ext'] = $this->ext;
  325. $param['add_upload'] = $this->data['key'];
  326. if (isset($this->data['cate'])) {
  327. $param['add_cate'] = $this->data['cate'];
  328. }
  329. if ($this->limit) {
  330. $param['add_width'] = $this->limit[0];
  331. $param['add_height'] = $this->limit[1];
  332. }
  333. if ($this->size) {
  334. $param['add_size'] = $this->size;
  335. }
  336. $param['add_state'] = 1;
  337. //file_put_contents(DEVER_PATH . 'data/test', var_export($param,true));
  338. if (isset($this->data['search']) && $this->data['search']) {
  339. $param['search'] = $this->data['search'];
  340. }
  341. $this->id = Dever::db('upload/file')->insert($param);
  342. }
  343. protected function img()
  344. {
  345. $this->img = isset($this->img) ? $this->img : new Img();
  346. $this->img->setType('im');
  347. return $this->img;
  348. }
  349. protected function getName()
  350. {
  351. $this->name($this->data['file']['name'])->path()->ext()->file();
  352. }
  353. protected function path()
  354. {
  355. # 覆盖原文件 检查文件是否存在
  356. $this->path = '';
  357. if ($this->config['cover'] == 1) {
  358. $info = Dever::db('upload/file')->one(array('name' => $this->name, 'upload' => $this->data['key']));
  359. if ($info && $info['file'] && strstr($info['file'], $this->name)) {
  360. # 如果存在,直接使用旧文件
  361. if (strstr($info['file'], 'http')) {
  362. $config = parse_url($info['file']);
  363. } else {
  364. $config['path'] = $info['file'];
  365. }
  366. $temp = explode($this->name, $config['path']);
  367. $this->path = ltrim($temp[0], '/');
  368. }
  369. }
  370. if (!$this->path) {
  371. $date = explode('-', date("Y-m-d"));
  372. $this->path = $this->config['id'] . '/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
  373. }
  374. return $this;
  375. }
  376. protected function name($name)
  377. {
  378. if (isset($this->data['name'])) {
  379. $this->name = $this->data['name'];
  380. } else {
  381. $this->name = md5($name);
  382. }
  383. return $this;
  384. }
  385. protected function ext()
  386. {
  387. $this->ext = $this->ext ? $this->ext : '.' . pathinfo($this->data['file']['name'], PATHINFO_EXTENSION);
  388. if ($this->ext == '.') {
  389. $this->ext = '.jpg';
  390. }
  391. return $this;
  392. }
  393. protected function yun()
  394. {
  395. $yun = $this->config['yun'];
  396. $this->host = $yun['host'];
  397. $this->data['host'] = $this->host;
  398. $this->getName();
  399. $file = $this->file;
  400. if ($this->config['cover'] == 3) {
  401. $this->name($this->output['file'] . '_' . microtime() . '_' . rand(0,1000))->file();
  402. $file = $this->file;
  403. }
  404. return array($yun, $file);
  405. }
  406. /**
  407. * getExt
  408. *
  409. * @return mixed
  410. */
  411. protected function getExt($filename)
  412. {
  413. if (isset($this->data['file']['type'])) {
  414. $ext = $this->getExtByMine($this->data['file']['type']);
  415. } elseif (function_exists('finfo_open')) {
  416. $finfo = finfo_open(FILEINFO_MIME); // 返回 mime 类型
  417. $code = finfo_file($finfo, $filename);
  418. finfo_close($finfo);
  419. $temp = explode(';', $code);
  420. $ext = $this->getExtByMine($temp[0]);
  421. } else {
  422. $ext = $this->getExtByByte($filename);
  423. }
  424. if (!$ext || $ext == 'txt' || $ext == 'exe') {
  425. if (isset($this->data['file']['type'])) {
  426. $ext = $this->getExtByMine($this->data['file']['type']);
  427. }
  428. if (!$ext || $ext == 'txt' || $ext == 'exe') {
  429. $ext = $this->getExtByByte($filename);
  430. }
  431. }
  432. return $ext;
  433. }
  434. public function delete()
  435. {
  436. @unlink($this->data['file']['tmp_name']);
  437. }
  438. /**
  439. * 根据mime类型获取文件扩展名
  440. *
  441. * @return mixed
  442. */
  443. protected function getExtByMine($mine)
  444. {
  445. $mine = trim($mine);
  446. $config = array
  447. (
  448. 'application/envoy' => 'evy',
  449. 'application/fractals' => 'fif',
  450. 'application/futuresplash' => 'spl',
  451. 'application/hta' => 'hta',
  452. 'application/internet-property-stream' => 'acx',
  453. 'application/mac-binhex40' => 'hqx',
  454. 'application/msword' => 'doc',
  455. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
  456. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
  457. 'video/x-m4v' => 'mp4',
  458. 'video/mp4' => 'mp4',
  459. 'application/octet-stream' => 'exe',
  460. 'application/oda' => 'oda',
  461. 'application/olescript' => 'axs',
  462. 'application/pdf' => 'pdf',
  463. 'application/pics-rules' => 'prf',
  464. 'application/pkcs10' => 'p10',
  465. 'application/pkix-crl' => 'crl',
  466. 'application/postscript' => 'ai',
  467. 'application/postscript' => 'eps',
  468. 'application/postscript' => 'ps',
  469. 'application/rtf' => 'rtf',
  470. 'application/set-payment-initiation' => 'setpay',
  471. 'application/set-registration-initiation' => 'setreg',
  472. 'application/vnd.ms-excel' => 'xls',
  473. 'application/vnd.ms-outlook' => 'msg',
  474. 'application/vnd.ms-pkicertstore' => 'sst',
  475. 'application/vnd.ms-pkiseccat' => 'cat',
  476. 'application/vnd.ms-pkistl' => 'stl',
  477. 'application/vnd.ms-powerpoint' => 'ppt',
  478. 'application/vnd.ms-project' => 'mpp',
  479. 'application/vnd.ms-works' => 'wps',
  480. 'application/winhlp' => 'hlp',
  481. 'application/x-bcpio' => 'bcpio',
  482. 'application/x-cdf' => 'cdf',
  483. 'application/x-compress' => 'z',
  484. 'application/x-compressed' => 'tgz',
  485. 'application/x-cpio' => 'cpio',
  486. 'application/x-csh' => 'csh',
  487. 'application/x-director' => 'dir',
  488. 'application/x-dvi' => 'dvi',
  489. 'application/x-gtar' => 'gtar',
  490. 'application/x-gzip' => 'gz',
  491. 'application/x-hdf' => 'hdf',
  492. 'application/x-internet-signup' => 'isp',
  493. 'application/x-iphone' => 'iii',
  494. 'application/x-javascript' => 'js',
  495. 'application/x-latex' => 'latex',
  496. 'application/x-msaccess' => 'mdb',
  497. 'application/x-mscardfile' => 'crd',
  498. 'application/x-msclip' => 'clp',
  499. 'application/x-msdownload' => 'dll',
  500. 'application/x-msmediaview' => 'mvb',
  501. 'application/x-msmetafile' => 'wmf',
  502. 'application/x-msmoney' => 'mny',
  503. 'application/x-mspublisher' => 'pub',
  504. 'application/x-msschedule' => 'scd',
  505. 'application/x-msterminal' => 'trm',
  506. 'application/x-mswrite' => 'wri',
  507. 'application/x-netcdf' => 'cdf',
  508. 'application/x-netcdf' => 'nc',
  509. 'application/x-perfmon' => 'pma',
  510. 'application/x-pkcs12' => 'p12',
  511. 'application/x-pkcs12' => 'pfx',
  512. 'application/x-pkcs7-certificates' => 'p7b',
  513. 'application/x-pkcs7-certreqresp' => 'p7r',
  514. 'application/x-pkcs7-mime' => 'p7c',
  515. 'application/x-pkcs7-signature' => 'p7s',
  516. 'application/x-sh' => 'sh',
  517. 'application/x-shar' => 'shar',
  518. 'application/x-shockwave-flash' => 'swf',
  519. 'application/x-stuffit' => 'sit',
  520. 'application/x-sv4cpio' => 'sv4cpio',
  521. 'application/x-sv4crc' => 'sv4crc',
  522. 'application/x-tar' => 'tar',
  523. 'application/x-tcl' => 'tcl',
  524. 'application/x-tex' => 'tex',
  525. 'application/x-texinfo' => 'texi',
  526. 'application/x-texinfo' => 'texinfo',
  527. 'application/x-troff' => 'roff',
  528. 'application/x-troff' => 't',
  529. 'application/x-troff' => 'tr',
  530. 'application/x-troff-man' => 'man',
  531. 'application/x-troff-me' => 'me',
  532. 'application/x-troff-ms' => 'ms',
  533. 'application/x-ustar' => 'ustar',
  534. 'application/x-wais-source' => 'src',
  535. 'application/x-x509-ca-cert' => 'cer',
  536. 'application/ynd.ms-pkipko' => 'pko',
  537. 'application/zip' => 'zip',
  538. 'audio/basic' => 'au',
  539. 'audio/basic' => 'snd',
  540. 'audio/mid' => 'mid',
  541. 'audio/mid' => 'rmi',
  542. 'audio/mpeg' => 'mp3',
  543. 'audio/mp3' => 'mp3',
  544. 'audio/x-aiff' => 'aif',
  545. 'audio/x-aiff' => 'aifc',
  546. 'audio/x-aiff' => 'aiff',
  547. 'audio/x-mpegurl' => 'm3u',
  548. 'audio/x-pn-realaudio' => 'ram',
  549. 'audio/x-wav' => 'wav',
  550. 'image/png' => 'png',
  551. 'image/bmp' => 'bmp',
  552. 'image/cis-cod' => 'cod',
  553. 'image/gif' => 'gif',
  554. 'image/ief' => 'ief',
  555. 'image/jpeg' => 'jpg',
  556. 'image/pipeg' => 'jfif',
  557. 'image/svg+xml' => 'svg',
  558. 'image/tiff' => 'tif',
  559. 'image/tiff' => 'tiff',
  560. 'image/x-cmu-raster' => 'ras',
  561. 'image/x-cmx' => 'cmx',
  562. 'image/x-icon' => 'ico',
  563. 'image/x-portable-anymap' => 'pnm',
  564. 'image/x-portable-bitmap' => 'pbm',
  565. 'image/x-portable-graymap' => 'pgm',
  566. 'image/x-portable-pixmap' => 'ppm',
  567. 'image/x-rgb' => 'rgb',
  568. 'image/x-xbitmap' => 'xbm',
  569. 'image/x-xpixmap' => 'xpm',
  570. 'image/x-xwindowdump' => 'xwd',
  571. 'message/rfc822' => 'mht',
  572. 'message/rfc822' => 'mhtml',
  573. 'message/rfc822' => 'nws',
  574. 'text/css' => 'css',
  575. 'text/h323' => '323',
  576. 'text/html' => 'html',
  577. 'text/iuls' => 'uls',
  578. 'text/plain' => 'txt',
  579. 'text/richtext' => 'rtx',
  580. 'text/scriptlet' => 'sct',
  581. 'text/tab-separated-values' => 'tsv',
  582. 'text/webviewhtml' => 'htt',
  583. 'text/x-component' => 'htc',
  584. 'text/x-setext' => 'etx',
  585. 'text/x-vcard' => 'vcf',
  586. 'video/mpeg' => 'mpeg',
  587. 'video/quicktime' => 'mov',
  588. 'video/x-ms-asf' => 'asx',
  589. 'video/x-msvideo' => 'avi',
  590. 'video/x-sgi-movie' => 'movie',
  591. 'x-world/x-vrml' => 'flr',
  592. 'application/x-rar' => 'rar',
  593. 'application/vnd.android.package-archive' => 'apk',
  594. 'audio/webm' => 'webm',
  595. 'video/webm' => 'webm',
  596. 'audio/x-m4a' => 'm4a',
  597. 'image/webp' => 'webp',
  598. );
  599. if (isset($config[$mine])) {
  600. return $config[$mine];
  601. } else {
  602. return false;
  603. }
  604. }
  605. /**
  606. * getExt
  607. *
  608. * @return mixed
  609. */
  610. protected function getExtByByte($filename)
  611. {
  612. $file = fopen($filename,"rb");
  613. $bin = fread($file,2);
  614. fclose($file);
  615. $strInfo = @unpack("c2chars",$bin);
  616. $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
  617. $fileType = '';
  618. switch ($typeCode) {
  619. case 7790:
  620. $fileType = 'exe';
  621. break;
  622. case 7784:
  623. $fileType = 'midi';
  624. break;
  625. case 8297:
  626. $fileType = 'rar';
  627. break;
  628. case 255216:
  629. $fileType = 'jpg';
  630. break;
  631. case 7173:
  632. $fileType = 'gif';
  633. break;
  634. case 13780:
  635. $fileType = 'png';
  636. break;
  637. case 6677:
  638. $fileType = 'bmp';
  639. break;
  640. case 6787:
  641. $fileType = 'swf';
  642. break;
  643. case 6063;
  644. $fileType = 'php|xml';
  645. break;
  646. case 6033:
  647. $fileType = 'html|htm|shtml';
  648. break;
  649. case 8075:
  650. $fileType = 'zip';
  651. break;
  652. case 6782:
  653. case 1310:
  654. $fileType = 'txt';
  655. break;
  656. case 4742:
  657. $fileType = 'js';
  658. break;
  659. case 8273:
  660. $fileType = 'wav';
  661. break;
  662. case 7368:
  663. $fileType = 'mp3';
  664. break;
  665. case 3780:
  666. $fileType = 'pdf';
  667. break;
  668. case 4545:
  669. $fileType = 'pem';
  670. break;
  671. case 7597:
  672. $fileType = 'fbx';
  673. break;
  674. default:
  675. $fileType = 'unknown'.$typeCode;
  676. break;
  677. }
  678. if ($strInfo['chars1'] == '-1' && $strInfo['chars2'] == '-40') {
  679. return 'jpg';
  680. }
  681. if ($strInfo['chars1'] == '-119' && $strInfo['chars2'] == '80') {
  682. return 'png';
  683. }
  684. if ($strInfo['chars1'] == '-48' && $strInfo['chars2'] == '-49') {
  685. return 'msi';
  686. }
  687. return $fileType;
  688. }
  689. }