今天要说的是如何从WordPress默认允许上传的文件类型中,禁止上传某些特定类型的文件,如WordPress默认允许上传 .exe 后缀名的可运行文件,那么我们怎么禁止用户在WordPress后台发表文章时上传 .exe 后缀名的文件呢?这就是本文要解答的问题。
首先,我们要知道WordPress支持上传哪些类型的文件,我们可以在当前主题的functions.php中插入以下php代码,然后打开博客首页,查看网页源代码,即可看到一个完整的支持列表(看完后,记得删除):
1print_r(wp_get_mime_types());
下面是以上代码输出的结果,这里供大家参考,也免了大家去写代码看结果。下面是WordPress默认允许上传的文件类型列表:
01// []中括号中的名称代表文件名后缀名/扩展名02// => 后面的名称代表的是后缀名所在应的文件MIME信息03Array04(05 [jpg|jpeg|jpe] => image/jpeg06 [gif] => image/gif07 [png] => image/png08 [bmp] => image/bmp09 [tif|tiff] => image/tiff10 [ico] => image/x-icon11 [asf|asx|wax|wmv|wmx] => video/asf12 [avi] => video/avi13 [divx] => video/divx14 <p style="text-indent:0px;text-align:center;"><embed src="http://www.luoxiao123.cn/wp-content/themes/ConcisePro/images/shortcodes/swf/flvideo.swf?auto=0&flv=" menu="false"quality="high" wmode="transparent" bgcolor="#ffffff" name="flvideo"allowscriptaccess="sameDomain" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer_cn" align="middle"height="315" width="560"></p> => video/x-flv15 [mov|qt] => video/quicktime16 [mpeg|mpg|mpe] => video/mpeg17 [mp4|m4v] => video/mp418 [ogv] => video/ogg19 [mkv] => video/x-matroska20 [txt|asc|c|cc|h] => text/plain21 [csv] => text/csv22 [tsv] => text/tab-separated-values23 [ics] => text/calendar24 [rtx] => text/richtext25 [css] => text/css26 [htm|html] => text/html27 <p><embed src="http://www.luoxiao123.cn/wp-content/themes/ConcisePro/images/shortcodes/swf/dewplayer.swf?mp3=&autostart=0&autoreplay=0" wmode="transparent" type="application/x-shockwave-flash"height="20" width="240"></p> => audio/mpeg28 [ra|ram] => audio/x-realaudio29 [wav] => audio/wav30 [ogg|oga] => audio/ogg31 [mid|midi] => audio/midi32 [wma] => audio/wma33 [mka] => audio/x-matroska34 [rtf] => application/rtf35 [js] => application/javascript36 [pdf] => application/pdf37 [swf] => application/x-shockwave-flash38 [class] => application/java39 [tar] => application/x-tar40 [zip] => application/zip41 [gz|gzip] => application/x-gzip42 [rar] => application/rar43 [7z] => application/x-7z-compressed44 [exe] => application/x-msdownload45 [doc] => application/msword46 [pot|pps|ppt] => application/vnd.ms-powerpoint47 [wri] => application/vnd.ms-write48 [xla|xls|xlt|xlw] => application/vnd.ms-excel49 [mdb] => application/vnd.ms-access50 [mpp] => application/vnd.ms-project51 [docx] => application/vnd.openxmlformats-officedocument.wordprocessingml.document52 [docm] => application/vnd.ms-word.document.macroEnabled.1253 [dotx] => application/vnd.openxmlformats-officedocument.wordprocessingml.template54 [dotm] => application/vnd.ms-word.template.macroEnabled.1255 [xlsx] => application/vnd.openxmlformats-officedocument.spreadsheetml.sheet56 [xlsm] => application/vnd.ms-excel.sheet.macroEnabled.1257 [xlsb] => application/vnd.ms-excel.sheet.binary.macroEnabled.1258 [xltx] => application/vnd.openxmlformats-officedocument.spreadsheetml.template59 [xltm] => application/vnd.ms-excel.template.macroEnabled.1260 [xlam] => application/vnd.ms-excel.addin.macroEnabled.1261 [pptx] => application/vnd.openxmlformats-officedocument.presentationml.presentation62 [pptm] => application/vnd.ms-powerpoint.presentation.macroEnabled.1263 [ppsx] => application/vnd.openxmlformats-officedocument.presentationml.slideshow64 [ppsm] => application/vnd.ms-powerpoint.slideshow.macroEnabled.1265 [potx] => application/vnd.openxmlformats-officedocument.presentationml.template66 [potm] => application/vnd.ms-powerpoint.template.macroEnabled.1267 [ppam] => application/vnd.ms-powerpoint.addin.macroEnabled.1268 [sldx] => application/vnd.openxmlformats-officedocument.presentationml.slide69 [sldm] => application/vnd.ms-powerpoint.slide.macroEnabled.1270 [onetoc|onetoc2|onetmp|onepkg] => application/onenote71 [odt] => application/vnd.oasis.opendocument.text72 [odp] => application/vnd.oasis.opendocument.presentation73 [ods] => application/vnd.oasis.opendocument.spreadsheet74 [odg] => application/vnd.oasis.opendocument.graphics75 [odc] => application/vnd.oasis.opendocument.chart76 [odb] => application/vnd.oasis.opendocument.database77 [odf] => application/vnd.oasis.opendocument.formula78 [wp|wpd] => application/wordperfect79)
上面的内容,大家看了可能眼花缭乱,其实只要记住,在每一行中,左边中括号中的名称是文件的后缀名(或者叫扩展名),右边 => 后面的名称代表的是后缀名所在应的文件MIME信息,这个我们不用管。
现在言归正传,如果想禁止用户在WordPress后台发表文章时上传特定后缀名的文件,我们可以在当前主题的functions.php中添加以下php代码:
1add_filter('upload_mimes', 'custom_upload_mimes');2 3function custom_upload_mimes($existing_mimes=array() ) {4 // 注意中括号中的名称,必须取自上面支持列表中中括号内的名称5 unset( $existing_mimes['exe'] ); //此处禁止了上传exe后缀名的文件6 7 return$existing_mimes; 8}
如果想禁止上传更多后缀名的文件,可以复制第5行的代码,粘贴到第5行代码以后,第7行代码之前,把其中的exe,改成要禁止上传的后缀名即可,如:
01add_filter('upload_mimes', 'custom_upload_mimes');02 03function custom_upload_mimes($existing_mimes=array() ) {04 // 注意中括号中的名称,必须取自上面支持列表中中括号的名称05 unset( $existing_mimes['exe'] ); //此处禁止了上传exe后缀名的可运行文件06 unset($existing_mimes['jpg|jpeg|jpe'] ); //此处禁止了上传jpg、jpeg和jpe后缀名的压缩文件07 unset( $existing_mimes['gif'] ); //此处禁止了上传gif后缀名的图片文件08 unset($existing_mimes['png'] ); //此处禁止了上传png后缀名的图片文件09 10 return$existing_mimes; 11}
经过此项设置,用户如果在后台上传禁止的文件类型,那么会得到这样的提示:
未经允许不得转载:445IT之家 » WordPress:禁止用户上传特定类型的文件