Core.php 21 KB

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