php上传常见文件类型对应的$_FILES["file"]["type"]。
xls
application/vnd.ms-excel
xlsx
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
ppt
application/vnd.ms-powerpoint
pptx
application/vnd.openxmlformats-officedocument.presentationml.presentation
doc
application/msword
docx
application/vnd.openxmlformats-officedocument.wordprocessingml.document
zip
application/x-zip-compressed
rar
application/x-zip-compressed
wmv
video/x-ms-wmv
mp3
audio/mpeg
mp4
video/mp4
img
image/gif
image/jpeg
image/pjpeg
有现成的代码你可以直接运行(在php环境下):html页面:
< html > < body > < form action = "upload_file.php" method = "post" enctype = "multipart/form-data" > < label for = "file" >Filename:</ label > < input type = "file" name = "file" id = "file" /> < br /> < input type = "submit" name = "submit" value = "Submit" /> </ form > </ body > </ html > |
upload_file.php:
<?php if ((( $_FILES [ "file" ][ "type" ] == "application/vnd.ms-excel" ) || ( $_FILES [ "file" ][ "type" ] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) ) ) { if ( $_FILES [ "file" ][ "error" ] > 0) { echo "Return Code: " . $_FILES [ "file" ][ "error" ] . "<br />" ; } else { echo "Upload: " . $_FILES [ "file" ][ "name" ] . "<br />" ; echo "Type: " . $_FILES [ "file" ][ "type" ] . "<br />" ; echo "Size: " . ( $_FILES [ "file" ][ "size" ] / 1024) . " Kb<br />" ; echo "Temp file: " . $_FILES [ "file" ][ "tmp_name" ] . "<br />" ; if ( file_exists ( "upload/" . $_FILES [ "file" ][ "name" ])) { echo $_FILES [ "file" ][ "name" ] . " already exists. " ; } else { move_uploaded_file( $_FILES [ "file" ][ "tmp_name" ], "upload/" . $_FILES [ "file" ][ "name" ]); echo "Stored in: " . "upload/" . $_FILES [ "file" ][ "name" ]; } } } else { echo "Invalid file" ; } ?> |
<input type="file" />浏览时只显示指定文件类型
<input type="file" accept="application/msword"><br><br>accept属性列表<br>
1.accept="application/msexcel"
2.accept="application/msword"
3.accept="application/pdf"
4.accept="application/poscript"
5.accept="application/rtf"
6.accept="application/x-zip-compressed"
7.accept="audio/basic"
8.accept="audio/x-aiff"
9.accept="audio/x-mpeg"
10.accept="audio/x-pn/realaudio"
11.accept="audio/x-waw"
12.accept="image/gif"
13.accept="image/jpeg"
14.accept="image/tiff"
15.accept="image/x-ms-bmp"
16.accept="image/x-photo-cd"
17.accept="image/x-png"
18.accept="image/x-portablebitmap"
19.accept="image/x-portable-greymap"
20.accept="image/x-portable-pixmap"
21.accept="image/x-rgb"
22.accept="text/html"
23.accept="text/plain"
24.accept="video/quicktime"
25.accept="video/x-mpeg2"
26.accept="video/x-msvideo"
这个就可以实现选择具体的数据类型,但是有兼容问题,我在测试的时候只有Opera,Chrome能用,火狐和IE都不兼容