日期:2014-05-17 浏览次数:20506 次
/** * Detect upload file type * * @param array $file * @return bool $flag */ 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; }