-
-
/**
- * Detect upload file type
- * 检测上传文件的excel文件类型
- * @param array $file
- * @return bool $flag
- * @site bbs.it-home.org
- */
- private function detectUploadFileMIME($file) {
- // 1.through the file extension judgement 03 or 07
- $flag = 0;
- $file_array = explode ( ".", $file ["name"] );
- $file_extension = strtolower ( array_pop ( $file_array ) );
-
- // 2.through the binary content to detect the file
- switch ($file_extension) {
- case "xls" :
- // 2003 excel
- $fh = fopen ( $file ["tmp_name"], "rb" );
- $bin = fread ( $fh, 8 );
- fclose ( $fh );
- $strinfo = @unpack ( "C8chars", $bin );
- $typecode = "";
- foreach ( $strinfo as $num ) {
- $typecode .= dechex ( $num );
- }
- if ($typecode == "d0cf11e0a1b11ae1") {
- $flag = 1;
- }
- break;
- case "xlsx" :
- // 2007 excel
- $fh = fopen ( $file ["tmp_name"], "rb" );
- $bin = fread ( $fh, 4 );
- fclose ( $fh );
- $strinfo = @unpack ( "C4chars", $bin );
- $typecode = "";
- foreach ( $strinfo as $num ) {
- $typecode .= dechex ( $num );
- }
- echo $typecode;
- if ($typecode == "504b34") {
- $flag = 1;
- }
- break;
- }
-
- // 3.return the flag
- return $flag;
- }
复制代码
|