上传文件,只判断后缀,貌似还不是很严谨;
/** * 判断文件是否合法 * @param $files * @param $arrCode * @return number|boolean */function checkFileType($files, $arrCode = array()){ $file = @fopen($files['tmp_name'], "rb"); $bin = @fread($file, 2); fclose($file); $str = @unpack('C2chars', $bin); $code = intval($str['chars1'].$str['chars2']); if(! is_array($arrCode)) return $code; return array_key_exists($code, $arrCode);}//Demo $arrCode = array( 6063 => 'php', 208207 => 'xls',);//如只允许上传php,和xls文件$result = checkFileType($_FILES['file'], $arrCode);