-
- /**
- * Determine uploaded file type
- * Edit bbs.it-home.org
- */
- function file_type($filename)
- {
- $file = fopen($filename, "rb");
- $bin = fread($file, 2); //只读2字节
- fclose($file);
- $strInfo = @unpack("C2chars", $bin);
- $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
- $fileType = '';
- switch ($typeCode)
- {
- case 7790:
- $fileType = 'exe';
- break;
- case 7784:
- $fileType = 'midi';
- break;
- case 8297:
- $fileType = 'rar';
- break;
- case 8075:
- $fileType = 'zip';
- break;
- case 255216:
- $fileType = 'jpg';
- break;
- case 7173:
- $fileType = 'gif';
- break;
- case 6677:
- $fileType = 'bmp';
- break;
- case 13780:
- $fileType = 'png';
- break;
- default:
- $fileType = 'unknown: '.$typeCode;
- }
-
- //Fix
- if ($strInfo['chars1']=='-1' AND $strInfo['chars2']=='-40' ) return 'jpg';
- if ($strInfo['chars1']=='-119' AND $strInfo['chars2']=='80' ) return 'png';
-
- return $fileType;
- }
-
- //调用
- echo file_type('start.php'); // 6063 or 6033
- ?>
-
复制代码
不知道反过来定义 6063或者6033 就是指php的话 是不是不够严谨啊。
上面的代码,对于构造假的图片的文件类型判断,不是很好使。
此时可以考虑使用getimagesize来判断,参考代码如下:
-
- /**
- * getimagesize determines the file type
- * Edit bbs.it-home.org
- */
- if(in_array($attach['ext'], array('jpg', 'jpeg', 'gif', 'png', 'swf', 'bmp')) && function_exists('getimagesize') && !@getimagesize($target))
- {
- unlink($target);
- upload_error('post_attachment_ext_notallowed', $attacharray);
- }
- ?>
复制代码
|