php 获取文件后缀

  /**
     * 获取文件后缀
   * $path 本地存储临时文件路径 *
*/ private function getFileType($path){ $fp=fopen($path,'r'); $bin = fread($fp, 2); //只读2字节 fclose($fp); $str_info = @unpack('C2chars', $bin);//Unpack data from binary string $type_code = intval($str_info['chars1'].$str_info['chars2']);// Get the integer value of a variable $file_type =''; switch ($type_code) { case 7790: $file_type = 'exe'; break; case 7784: $file_type = 'midi'; break; case 8075: $file_type = 'zip'; break; case 8297: $file_type = 'rar'; break; case 255216: $file_type = 'jpg'; break; case 7173: $file_type = 'gif'; break; case 6677: $file_type = 'bmp'; break; case 13780: $file_type = 'png'; break; default: $file_type = 'unknown'; break; } return $file_type; }

 

posted @ 2017-11-10 17:47  一个人的孤独自白  阅读(279)  评论(0编辑  收藏  举报