今天要說的是如何從WordPress默認(rèn)允許上傳的文件類型中,禁止上傳某些特定類型的文件,如WordPress默認(rèn)允許上傳 .exe 后綴名的可運(yùn)行文件,那么我們怎么禁止用戶在WordPress后臺發(fā)表文章時上傳 .exe 后綴名的文件呢?這就是本文要解答的問題。
首先,我們要知道WordPress支持上傳哪些類型的文件,我們可以在當(dāng)前主題的functions.php中插入以下php代碼,然后打開博客首頁,查看網(wǎng)頁源代碼,即可看到一個完整的支持列表(看完后,記得刪除):
1print_r(wp_get_mime_types());
下面是以上代碼輸出的結(jié)果,這里供大家參考,也免了大家去寫代碼看結(jié)果。下面是WordPress默認(rèn)允許上傳的文件類型列表:
01// []中括號中的名稱代表文件名后綴名/擴(kuò)展名02// => 后面的名稱代表的是后綴名所在應(yīng)的文件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)
上面的內(nèi)容,大家看了可能眼花繚亂,其實只要記住,在每一行中,左邊中括號中的名稱是文件的后綴名(或者叫擴(kuò)展名),右邊 => 后面的名稱代表的是后綴名所在應(yīng)的文件MIME信息,這個我們不用管。
現(xiàn)在言歸正傳,如果想禁止用戶在WordPress后臺發(fā)表文章時上傳特定后綴名的文件,我們可以在當(dāng)前主題的functions.php中添加以下php代碼:
1add_filter('upload_mimes', 'custom_upload_mimes');2 3function custom_upload_mimes($existing_mimes=array() ) {4 // 注意中括號中的名稱,必須取自上面支持列表中中括號內(nèi)的名稱5 unset( $existing_mimes['exe'] ); //此處禁止了上傳exe后綴名的文件6 7 return$existing_mimes; 8}
如果想禁止上傳更多后綴名的文件,可以復(fù)制第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后綴名的可運(yùn)行文件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}
經(jīng)過此項設(shè)置,用戶如果在后臺上傳禁止的文件類型,那么會得到這樣的提示:
未經(jīng)允許不得轉(zhuǎn)載:445IT之家 » WordPress:禁止用戶上傳特定類型的文件