Core.php 20 KB

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