WordPress Hook – 過濾上傳文件(upload_mimes)
客戶或使用者在WordPress編輯文章時,很有可能會上傳一些很大或是有危險性的檔案
有的時候網站是使用圖床的方式在維護
此時就必須強制設定一些方式,讓使用者無法上傳某些檔案
這裏介紹使用黑名單的方式過濾(當然你可以換白名單的方式)
範例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | function __upload_mimes($existing_mimes=array()){ foreach($existing_mimes as $i => $row){ switch( $row ){ case 'audio/mpeg': // mp3|m4a|m4b case 'audio/wav': // wav case 'audio/ogg': // ogg|oga case 'audio/midi': // mid|midi case 'audio/x-ms-wma': // wma case 'video/x-ms-wmv': // wmv case 'video/avi': // avi case 'video/x-flv': // flv case 'video/quicktime': // mov|qt case 'video/mpeg': // mpeg|mpg|mpe case 'video/mp4': // mp4|m4v case 'video/ogg': // ogv case 'video/webm': // webm case 'video/x-matroska': // mkv case 'video/3gpp': // 3gp|3gpp case 'video/3gpp2': // 3g2|3gp2 unset($existing_mimes[$i]); break; } } return $existing_mimes; } add_filter ( 'upload_mimes', '__upload_mimes', 10, 1); |
關於MIME的部分,您可以參考 /wp-includes/function.php
搜尋「function wp_get_mime_types()」這個函數
臉書留言
一般留言