PHP文件上传类型后辍名对应mine对照表

PHP文件上传类型后辍名对应mine对照表

使用PHP脚本上传文件时需要将文件格式处理为PHP所认识的文件类型,例如(.jpg的文件格式为image/jpeg)。这个格式的判断首先是由浏览器完成的,浏览器通过表单的提交判断是某类文件,再提交给php进行处理。

有时候不同浏览器对文件类型的定义不相同,因此有时候需要对不同的浏览器做判断。其实可以将不同浏览器的类型都加入到判断中。

下面提供一张IE和火狐浏览器的文件类型对照表:

ie火狐

id

后缀名

php识别出的文件类型

0gifimage/gif
1jpgimage/jpeg
2pngimage/png
3image/bmp
4psdapplication/octet-stream
5icoimage/x-icon
6rarapplication/octet-stream
7zipapplication/zip
87zapplication/octet-stream
9exeapplication/octet-stream
10avivideo/avi
11rmvbapplication/vnd.rn-realmedia-vbr
123gpapplication/octet-stream
13flvapplication/octet-stream
14mp3audio/mpeg
15wavaudio/wav
16krcapplication/octet-stream
17lrcapplication/octet-stream
18txttext/plain
19docapplication/msword
20xlsapplication/vnd.ms-excel
21pptapplication/vnd.ms-powerpoint
22pdfapplication/pdf
23chmapplication/octet-stream
24mdbapplication/msaccess
25sqlapplication/octet-stream
26conapplication/octet-stream
27logtext/plain
28datapplication/octet-stream
29iniapplication/octet-stream
30phpapplication/octet-stream
31htmltext/html
32htmtext/html
33ttfapplication/octet-stream
34fonapplication/octet-stream
35jsapplication/x-javascript
36xmltext/xml
37dllapplication/octet-stream
38dllapplication/octet-stream

id

后缀名

php识别出的文件类型

0gifimage/gif
1jpgimage/pjpeg
2pngimage/x-png
3image/bmp
4psdapplication/octet-stream
5icoimage/x-icon
6rarapplication/octet-stream
7zipapplication/x-zip-compressed
87zapplication/octet-stream
9exeapplication/octet-stream
10avivideo/avi
11rmvbapplication/vnd.rn-realmedia-vbr
123gpapplication/octet-stream
13flvapplication/octet-stream
14mp3audio/mpeg
15wavaudio/wav
16krcapplication/octet-stream
17lrcapplication/octet-stream
18txttext/plain
19docapplication/msword
20xlsapplication/vnd.ms-excel
21pptapplication/vnd.ms-powerpoint
22pdfapplication/pdf
23chmapplication/octet-stream
24mdbapplication/msaccess
25sqltext/plain
26conapplication/octet-stream
27logtext/plain
28dattext/plain
29iniapplication/octet-stream
30phpapplication/octet-stream
31htmltext/html
32htmtext/html
33ttfapplication/octet-stream
34fonapplication/octet-stream
35jstext/html
36xmltext/xml
37dllapplication/octet-stream
38classapplication/java

下面看一个实例

代码如下

$temppath=$upfile['tmp_name'];

$fileinfo=pathinfo($upfile['name']);

$extension=$upfile['type'];

//echo $extension;

//exit;

switch( $extension )

{

case 'application/msword':

$extension ='doc';

break;

case 'application/vnd.ms-excel':

$extension ='xls';

break;

case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':

$extension ='docx';

break;

case 'application/vnd.ms-powerpoint':

$extension ='ppt';

break;

case 'application/pdf':

$extension ='pdf';

break;

case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':

$extension ='xlsx';

break;

default:

die('只允许上传doc,docx,xls,pdf,ppt文件 <a href="a.php">重新上传</a>');

}

上面实例就限制了只能上传doc,docx,xls,pdf,ppt了,这样如果有人想利用后缀名上传其它如php或asp文件就存在会提示上传文件不合法。