Core.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. } elseif (strstr($this->data['file'], 'content,')) {
  196. $file = str_replace('content,', '', $this->data['file']);
  197. $this->data['content'] = true;
  198. } else {
  199. $name = urldecode($this->data['file']);
  200. }
  201. $this->data['file'] = array();
  202. if (isset($this->data['content']) || (!isset($this->data['cropper']) && strstr($name, 'http') && !$this->yun)) {
  203. $this->base64 = false;
  204. $this->root();
  205. //header('Content-type: text/json; charset=utf-8');
  206. $path = Dever::path($this->base, 'tmp/');
  207. $filename = $name;
  208. if (strstr($filename, '?')) {
  209. $temp = explode('?', $filename);
  210. $filename = $temp[0];
  211. }
  212. if (isset($this->data['ext']) && $this->data['ext']) {
  213. $this->ext = '.' . $this->data['ext'];
  214. } else {
  215. $this->ext = '.' . pathinfo($filename, PATHINFO_EXTENSION);
  216. }
  217. if (isset($this->data['name']) && $this->data['name']) {
  218. $this->data['file']['name'] = 'Tmp' . sha1($this->data['name']);
  219. } else {
  220. $this->data['file']['name'] = 'Tmp' . sha1($filename);
  221. }
  222. $this->data['file']['tmp_name'] = $path . $this->data['file']['name'];
  223. if (!is_file($this->data['file']['tmp_name'])) {
  224. if (strstr($name, 'tp=webp')) {
  225. $name = str_replace('tp=webp', 'tp=jpeg', $name);
  226. } elseif (strstr($name, '.webp')) {
  227. $name = str_replace('.webp', '.jpg', $name);
  228. }
  229. if (!isset($file)) {
  230. $file = Dever::curl($name);
  231. }
  232. if ($this->ext != '.mp4' && stristr($file, 'webp')) {
  233. # 将webp图片转成jpg
  234. $this->ext = '.jpg';
  235. }
  236. if (!isset($file)) {
  237. $this->output['status'] = -1;
  238. $this->output['message'] = '错误的文件来源';
  239. return $this->output;
  240. }
  241. $state = file_put_contents($this->data['file']['tmp_name'], $file);
  242. if (!$state) {
  243. $this->delete();
  244. return $this->output;
  245. }
  246. }
  247. $this->data['file']['size'] = filesize($this->data['file']['tmp_name']);
  248. if ($this->data['file']['size'] <= 0) {
  249. $this->delete();
  250. return $this->output;
  251. }
  252. } elseif (isset($file)) {
  253. $this->data['file']['name'] = $name;
  254. $this->data['file']['tmp_name'] = $file;
  255. } else {
  256. $this->data['file']['name'] = $name;
  257. $this->data['file']['tmp_name'] = $name;
  258. }
  259. if (isset($type)) {
  260. $this->data['file']['type'] = $type;
  261. }
  262. if (isset($size)) {
  263. $this->data['file']['size'] = $size;
  264. }
  265. if (isset($this->data['param'])) {
  266. $this->limit = array($this->data['param']['param_w'], $this->data['param']['param_h']);
  267. if (isset($this->data['name'])) {
  268. $param = implode('_', array_values($this->data['param']));
  269. $this->data['file']['name'] = '.' . $this->data['name'] . $param;
  270. if ($this->base64) {
  271. $this->data['name'] = $this->data['file']['name'];
  272. } else {
  273. $filename = md5($this->data['pic']) . '.' . pathinfo($this->data['pic'], PATHINFO_EXTENSION);
  274. $this->path();
  275. $file = Dever::path($this->base . $this->data['key'] . $this->path, $filename);
  276. if (!is_file($file)) {
  277. $content = Dever::curl($name);
  278. file_put_contents($file, $content);
  279. }
  280. $this->data['name'] = $filename . $this->data['file']['name'];
  281. }
  282. $this->ext = '.jpg';
  283. }
  284. }
  285. } else {
  286. header("Content-type: application/json; charset=utf-8");
  287. }
  288. $this->check(array('handle', 'file', 'key', 'type', 'size'));
  289. if ($this->output['status'] == -1) {
  290. $this->delete();
  291. return $this->output;
  292. }
  293. $this->save();
  294. if ($this->output['status'] == -1) {
  295. return $this->output;
  296. }
  297. if (isset($this->handle) && is_array($this->handle)) {
  298. foreach ($this->handle as $k => $v) {
  299. $method = 'handle_' . $v[0];
  300. if (method_exists($this, $method)) {
  301. $this->$method($v);
  302. }
  303. $this->$method($v[1]);
  304. }
  305. } elseif (isset($this->config['alter']) && $this->config['alter']) {
  306. parse_str($this->config['alter'], $handle);
  307. if ($handle) {
  308. foreach ($handle as $k => $v) {
  309. $method = 'handle_' . $k;
  310. if (method_exists($this, $method)) {
  311. $this->$method($v);
  312. }
  313. }
  314. }
  315. }
  316. $this->output['uploaded'] = true;
  317. $this->output['status'] = 1;
  318. $this->output['name'] = $this->data['file']['name'];
  319. $this->output['fid'] = $this->id;
  320. if ($this->limit) {
  321. $this->output['width'] = $this->limit[0];
  322. $this->output['height'] = $this->limit[1];
  323. }
  324. if ($this->size) {
  325. $this->output['size'] = $this->size;
  326. }
  327. return $this->output;
  328. }
  329. protected function update($id)
  330. {
  331. $param['set_name'] = $this->name;
  332. $param['set_source_name'] = $this->data['file']['name'];
  333. $param['set_file'] = $this->file;
  334. $param['set_key'] = md5($this->output['url']);
  335. $param['set_ext'] = $this->ext;
  336. if (isset($this->data['cate'])) {
  337. $param['set_cate'] = $this->data['cate'];
  338. }
  339. if ($this->limit) {
  340. $param['set_width'] = $this->limit[0];
  341. $param['set_height'] = $this->limit[1];
  342. }
  343. if ($this->size) {
  344. $param['set_size'] = $this->size;
  345. }
  346. if (isset($this->data['search']) && $this->data['search']) {
  347. $param['search'] = $this->data['search'];
  348. }
  349. $this->id = $param['where_id'] = $id;
  350. Dever::db('upload/file')->update($param);
  351. }
  352. protected function insert()
  353. {
  354. $param['add_name'] = $this->name;
  355. $param['add_source_name'] = $this->data['file']['name'];
  356. $param['add_file'] = $this->file;
  357. $param['add_key'] = md5($this->output['url']);
  358. $param['add_ext'] = $this->ext;
  359. $param['add_upload'] = $this->data['key'];
  360. if (isset($this->data['cate'])) {
  361. $param['add_cate'] = $this->data['cate'];
  362. }
  363. if ($this->limit) {
  364. $param['add_width'] = $this->limit[0];
  365. $param['add_height'] = $this->limit[1];
  366. }
  367. if ($this->size) {
  368. $param['add_size'] = $this->size;
  369. }
  370. $param['add_state'] = 1;
  371. //file_put_contents(DEVER_PATH . 'data/test', var_export($param,true));
  372. if (isset($this->data['search']) && $this->data['search']) {
  373. $param['search'] = $this->data['search'];
  374. }
  375. $this->id = Dever::db('upload/file')->insert($param);
  376. }
  377. protected function img()
  378. {
  379. $this->img = isset($this->img) ? $this->img : new Img();
  380. $this->img->setType('im');
  381. return $this->img;
  382. }
  383. protected function getName()
  384. {
  385. $this->name($this->data['file']['name'])->path()->ext()->file();
  386. }
  387. protected function path()
  388. {
  389. # 覆盖原文件 检查文件是否存在
  390. $this->path = '';
  391. if ($this->config['cover'] == 1) {
  392. $info = Dever::db('upload/file')->one(array('name' => $this->name, 'upload' => $this->data['key']));
  393. if ($info && $info['file'] && strstr($info['file'], $this->name)) {
  394. # 如果存在,直接使用旧文件
  395. if (strstr($info['file'], 'http')) {
  396. $config = parse_url($info['file']);
  397. } else {
  398. $config['path'] = $info['file'];
  399. }
  400. $temp = explode($this->name, $config['path']);
  401. $this->path = ltrim($temp[0], '/');
  402. }
  403. }
  404. if (!$this->path) {
  405. $date = explode('-', date("Y-m-d"));
  406. if (isset($this->data['id']) && $this->data['id']) {
  407. $this->path = $this->config['id'] . '/' . Dever::pathId($this->data['id']);
  408. } else {
  409. $this->path = $this->config['id'] . '/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
  410. }
  411. }
  412. return $this;
  413. }
  414. protected function name($name)
  415. {
  416. if (isset($this->data['name'])) {
  417. $this->name = $this->data['name'];
  418. } else {
  419. $this->name = md5($name);
  420. }
  421. return $this;
  422. }
  423. protected function ext()
  424. {
  425. $this->ext = $this->ext ? $this->ext : '.' . pathinfo($this->data['file']['name'], PATHINFO_EXTENSION);
  426. if ($this->ext == '.') {
  427. $this->ext = '.jpg';
  428. }
  429. return $this;
  430. }
  431. protected function yun()
  432. {
  433. $yun = $this->config['yun'];
  434. $this->host = $yun['host'];
  435. $this->data['host'] = $this->host;
  436. $this->getName();
  437. $file = $this->file;
  438. if ($this->config['cover'] == 3) {
  439. $this->name($this->output['file'] . '_' . microtime() . '_' . rand(0,1000))->file();
  440. $file = $this->file;
  441. }
  442. return array($yun, $file);
  443. }
  444. /**
  445. * getExt
  446. *
  447. * @return mixed
  448. */
  449. protected function getExt($filename)
  450. {
  451. if (strstr($filename, 'http')) {
  452. $temp = explode('.', $filename);
  453. $ext = $temp[count($temp)-1];
  454. } elseif (isset($this->data['file']['type'])) {
  455. $ext = $this->getExtByMine($this->data['file']['type']);
  456. } elseif (function_exists('finfo_open')) {
  457. $finfo = finfo_open(FILEINFO_MIME); // 返回 mime 类型
  458. $code = finfo_file($finfo, $filename);
  459. finfo_close($finfo);
  460. $temp = explode(';', $code);
  461. $ext = $this->getExtByMine($temp[0]);
  462. } else {
  463. $ext = $this->getExtByByte($filename);
  464. }
  465. if (!$ext || $ext == 'txt' || $ext == 'exe') {
  466. if (isset($this->data['file']['type'])) {
  467. $ext = $this->getExtByMine($this->data['file']['type']);
  468. }
  469. if (!$ext || $ext == 'txt' || $ext == 'exe') {
  470. $ext = $this->getExtByByte($filename);
  471. }
  472. }
  473. return $ext;
  474. }
  475. public function delete()
  476. {
  477. @unlink($this->data['file']['tmp_name']);
  478. }
  479. /**
  480. * 根据mime类型获取文件扩展名
  481. *
  482. * @return mixed
  483. */
  484. protected function getExtByMine($mine)
  485. {
  486. $mine = trim($mine);
  487. $config = array
  488. (
  489. 'application/envoy' => 'evy',
  490. 'application/fractals' => 'fif',
  491. 'application/futuresplash' => 'spl',
  492. 'application/hta' => 'hta',
  493. 'application/internet-property-stream' => 'acx',
  494. 'application/mac-binhex40' => 'hqx',
  495. 'application/msword' => 'doc',
  496. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
  497. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
  498. 'video/x-m4v' => 'mp4',
  499. 'video/mp4' => 'mp4',
  500. 'application/octet-stream' => 'exe',
  501. 'application/oda' => 'oda',
  502. 'application/olescript' => 'axs',
  503. 'application/pdf' => 'pdf',
  504. 'application/pics-rules' => 'prf',
  505. 'application/pkcs10' => 'p10',
  506. 'application/pkix-crl' => 'crl',
  507. 'application/postscript' => 'ai',
  508. 'application/postscript' => 'eps',
  509. 'application/postscript' => 'ps',
  510. 'application/rtf' => 'rtf',
  511. 'application/set-payment-initiation' => 'setpay',
  512. 'application/set-registration-initiation' => 'setreg',
  513. 'application/vnd.ms-excel' => 'xls',
  514. 'application/vnd.ms-outlook' => 'msg',
  515. 'application/vnd.ms-pkicertstore' => 'sst',
  516. 'application/vnd.ms-pkiseccat' => 'cat',
  517. 'application/vnd.ms-pkistl' => 'stl',
  518. 'application/vnd.ms-powerpoint' => 'ppt',
  519. 'application/vnd.ms-project' => 'mpp',
  520. 'application/vnd.ms-works' => 'wps',
  521. 'application/winhlp' => 'hlp',
  522. 'application/x-bcpio' => 'bcpio',
  523. 'application/x-cdf' => 'cdf',
  524. 'application/x-compress' => 'z',
  525. 'application/x-compressed' => 'tgz',
  526. 'application/x-cpio' => 'cpio',
  527. 'application/x-csh' => 'csh',
  528. 'application/x-director' => 'dir',
  529. 'application/x-dvi' => 'dvi',
  530. 'application/x-gtar' => 'gtar',
  531. 'application/x-gzip' => 'gz',
  532. 'application/x-hdf' => 'hdf',
  533. 'application/x-internet-signup' => 'isp',
  534. 'application/x-iphone' => 'iii',
  535. 'application/x-javascript' => 'js',
  536. 'application/x-latex' => 'latex',
  537. 'application/x-msaccess' => 'mdb',
  538. 'application/x-mscardfile' => 'crd',
  539. 'application/x-msclip' => 'clp',
  540. 'application/x-msdownload' => 'dll',
  541. 'application/x-msmediaview' => 'mvb',
  542. 'application/x-msmetafile' => 'wmf',
  543. 'application/x-msmoney' => 'mny',
  544. 'application/x-mspublisher' => 'pub',
  545. 'application/x-msschedule' => 'scd',
  546. 'application/x-msterminal' => 'trm',
  547. 'application/x-mswrite' => 'wri',
  548. 'application/x-netcdf' => 'cdf',
  549. 'application/x-netcdf' => 'nc',
  550. 'application/x-perfmon' => 'pma',
  551. 'application/x-pkcs12' => 'p12',
  552. 'application/x-pkcs12' => 'pfx',
  553. 'application/x-pkcs7-certificates' => 'p7b',
  554. 'application/x-pkcs7-certreqresp' => 'p7r',
  555. 'application/x-pkcs7-mime' => 'p7c',
  556. 'application/x-pkcs7-signature' => 'p7s',
  557. 'application/x-sh' => 'sh',
  558. 'application/x-shar' => 'shar',
  559. 'application/x-shockwave-flash' => 'swf',
  560. 'application/x-stuffit' => 'sit',
  561. 'application/x-sv4cpio' => 'sv4cpio',
  562. 'application/x-sv4crc' => 'sv4crc',
  563. 'application/x-tar' => 'tar',
  564. 'application/x-tcl' => 'tcl',
  565. 'application/x-tex' => 'tex',
  566. 'application/x-texinfo' => 'texi',
  567. 'application/x-texinfo' => 'texinfo',
  568. 'application/x-troff' => 'roff',
  569. 'application/x-troff' => 't',
  570. 'application/x-troff' => 'tr',
  571. 'application/x-troff-man' => 'man',
  572. 'application/x-troff-me' => 'me',
  573. 'application/x-troff-ms' => 'ms',
  574. 'application/x-ustar' => 'ustar',
  575. 'application/x-wais-source' => 'src',
  576. 'application/x-x509-ca-cert' => 'cer',
  577. 'application/ynd.ms-pkipko' => 'pko',
  578. 'application/zip' => 'zip',
  579. 'audio/basic' => 'au',
  580. 'audio/basic' => 'snd',
  581. 'audio/mid' => 'mid',
  582. 'audio/mid' => 'rmi',
  583. 'audio/mpeg' => 'mp3',
  584. 'audio/mp3' => 'mp3',
  585. 'audio/x-aiff' => 'aif',
  586. 'audio/x-aiff' => 'aifc',
  587. 'audio/x-aiff' => 'aiff',
  588. 'audio/x-mpegurl' => 'm3u',
  589. 'audio/x-pn-realaudio' => 'ram',
  590. 'audio/x-wav' => 'wav',
  591. 'image/png' => 'png',
  592. 'image/bmp' => 'bmp',
  593. 'image/cis-cod' => 'cod',
  594. 'image/gif' => 'gif',
  595. 'image/ief' => 'ief',
  596. 'image/jpeg' => 'jpg',
  597. 'image/pipeg' => 'jfif',
  598. 'image/svg+xml' => 'svg',
  599. 'image/tiff' => 'tif',
  600. 'image/tiff' => 'tiff',
  601. 'image/x-cmu-raster' => 'ras',
  602. 'image/x-cmx' => 'cmx',
  603. 'image/x-icon' => 'ico',
  604. 'image/x-portable-anymap' => 'pnm',
  605. 'image/x-portable-bitmap' => 'pbm',
  606. 'image/x-portable-graymap' => 'pgm',
  607. 'image/x-portable-pixmap' => 'ppm',
  608. 'image/x-rgb' => 'rgb',
  609. 'image/x-xbitmap' => 'xbm',
  610. 'image/x-xpixmap' => 'xpm',
  611. 'image/x-xwindowdump' => 'xwd',
  612. 'message/rfc822' => 'mht',
  613. 'message/rfc822' => 'mhtml',
  614. 'message/rfc822' => 'nws',
  615. 'text/css' => 'css',
  616. 'text/h323' => '323',
  617. 'text/html' => 'html',
  618. 'text/iuls' => 'uls',
  619. 'text/plain' => 'txt',
  620. 'text/richtext' => 'rtx',
  621. 'text/scriptlet' => 'sct',
  622. 'text/tab-separated-values' => 'tsv',
  623. 'text/webviewhtml' => 'htt',
  624. 'text/x-component' => 'htc',
  625. 'text/x-setext' => 'etx',
  626. 'text/x-vcard' => 'vcf',
  627. 'video/mpeg' => 'mpeg',
  628. 'video/quicktime' => 'mov',
  629. 'video/x-ms-asf' => 'asx',
  630. 'video/x-msvideo' => 'avi',
  631. 'video/x-sgi-movie' => 'movie',
  632. 'x-world/x-vrml' => 'flr',
  633. 'application/x-rar' => 'rar',
  634. 'application/vnd.android.package-archive' => 'apk',
  635. 'audio/webm' => 'webm',
  636. 'video/webm' => 'webm',
  637. 'audio/x-m4a' => 'm4a',
  638. 'image/webp' => 'webp',
  639. );
  640. if (isset($config[$mine])) {
  641. return $config[$mine];
  642. } else {
  643. return false;
  644. }
  645. }
  646. /**
  647. * getExt
  648. *
  649. * @return mixed
  650. */
  651. protected function getExtByByte($filename)
  652. {
  653. $file = fopen($filename,"rb");
  654. $bin = fread($file,2);
  655. fclose($file);
  656. $strInfo = @unpack("c2chars",$bin);
  657. $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
  658. $fileType = '';
  659. switch ($typeCode) {
  660. case 7790:
  661. $fileType = 'exe';
  662. break;
  663. case 7784:
  664. $fileType = 'midi';
  665. break;
  666. case 8297:
  667. $fileType = 'rar';
  668. break;
  669. case 255216:
  670. $fileType = 'jpg';
  671. break;
  672. case 7173:
  673. $fileType = 'gif';
  674. break;
  675. case 13780:
  676. $fileType = 'png';
  677. break;
  678. case 6677:
  679. $fileType = 'bmp';
  680. break;
  681. case 6787:
  682. $fileType = 'swf';
  683. break;
  684. case 6063;
  685. $fileType = 'php|xml';
  686. break;
  687. case 6033:
  688. $fileType = 'html|htm|shtml';
  689. break;
  690. case 8075:
  691. $fileType = 'zip';
  692. break;
  693. case 6782:
  694. case 1310:
  695. $fileType = 'txt';
  696. break;
  697. case 4742:
  698. $fileType = 'js';
  699. break;
  700. case 8273:
  701. $fileType = 'wav';
  702. break;
  703. case 7368:
  704. $fileType = 'mp3';
  705. break;
  706. case 3780:
  707. $fileType = 'pdf';
  708. break;
  709. case 4545:
  710. $fileType = 'pem';
  711. break;
  712. case 7597:
  713. $fileType = 'fbx';
  714. break;
  715. default:
  716. $fileType = 'unknown'.$typeCode;
  717. break;
  718. }
  719. if ($strInfo['chars1'] == '-1' && $strInfo['chars2'] == '-40') {
  720. return 'jpg';
  721. }
  722. if ($strInfo['chars1'] == '-119' && $strInfo['chars2'] == '80') {
  723. return 'png';
  724. }
  725. if ($strInfo['chars1'] == '-48' && $strInfo['chars2'] == '-49') {
  726. return 'msi';
  727. }
  728. return $fileType;
  729. }
  730. }