Core.php 19 KB

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