不知道大家遇没遇到过上传某种类型的文件时,收到 WordPress 的提示说出于安全考虑不支持此类型文件上传的情况。对于国内用户来说,可能比较常见的就是.rar 扩展名的文件了,WordPress 默认允许上传的文件类型里就不包括.rar 扩展名的压缩包,如果你用的插件或者主题没有额外设置的话,那么毫无意外的你上传 RAR 压缩包时就会看到这个提示了。
WordPress 为了安全考虑,限制了能上传的文件格式这点无可厚非,毕竟如果你安全防护没做到家,你也不想别人能随随便便的往你网站里上传恶意的 PHP 文件还有 JS 脚本之类的吧?不过有时候一些无关紧要的,或者是某种自定义的特殊文件格式需要上传的话,为了方便还是让 WordPress 允许上传的好。
要让 WordPress 增加允许上传的文件格式,也是挺简单的,不过在那之前,我们可以先了解一下 WordPress 默认支持上传的文件格式,默认允许上传的文件格式配置位于你的网站目录/wp-includes/functions.php 中,默认支持上传的文件格式如下:
return apply_filters( 'mime_types', array( // Image formats. 'jpg|jpeg|jpe' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'bmp' => 'image/bmp', 'tiff|tif' => 'image/tiff', 'ico' => 'image/x-icon', // Video formats. 'asf|asx' => 'video/x-ms-asf', 'wmv' => 'video/x-ms-wmv', 'wmx' => 'video/x-ms-wmx', 'wm' => 'video/x-ms-wm', 'avi' => 'video/avi', 'divx' => 'video/divx', 'flv' => 'video/x-flv', 'mov|qt' => 'video/quicktime', 'mpeg|mpg|mpe' => 'video/mpeg', 'mp4|m4v' => 'video/mp4', 'ogv' => 'video/ogg', 'webm' => 'video/webm', 'mkv' => 'video/x-matroska', '3gp|3gpp' => 'video/3gpp', // Can also be audio '3g2|3gp2' => 'video/3gpp2', // Can also be audio // Text formats. 'txt|asc|c|cc|h|srt' => 'text/plain', 'csv' => 'text/csv', 'tsv' => 'text/tab-separated-values', 'ics' => 'text/calendar', 'rtx' => 'text/richtext', 'css' => 'text/css', 'htm|html' => 'text/html', 'vtt' => 'text/vtt', 'dfxp' => 'application/ttaf+xml', // Audio formats. 'mp3|m4a|m4b' => 'audio/mpeg', 'aac' => 'audio/aac', 'ra|ram' => 'audio/x-realaudio', 'wav' => 'audio/wav', 'ogg|oga' => 'audio/ogg', 'flac' => 'audio/flac', 'mid|midi' => 'audio/midi', 'wma' => 'audio/x-ms-wma', 'wax' => 'audio/x-ms-wax', 'mka' => 'audio/x-matroska', // Misc application formats. 'rtf' => 'application/rtf', 'js' => 'application/javascript', 'pdf' => 'application/pdf', 'swf' => 'application/x-shockwave-flash', 'class' => 'application/java', 'tar' => 'application/x-tar', 'zip' => 'application/zip', 'gz|gzip' => 'application/x-gzip', 'rar' => 'application/rar', '7z' => 'application/x-7z-compressed', 'exe' => 'application/x-msdownload', 'psd' => 'application/octet-stream', 'xcf' => 'application/octet-stream', // MS Office formats. 'doc' => 'application/msword', 'pot|pps|ppt' => 'application/vnd.ms-powerpoint', 'wri' => 'application/vnd.ms-write', 'xla|xls|xlt|xlw' => 'application/vnd.ms-excel', 'mdb' => 'application/vnd.ms-access', 'mpp' => 'application/vnd.ms-project', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'docm' => 'application/vnd.ms-word.document.macroEnabled.12', 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12', 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12', 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12', 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12', 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', 'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12', 'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote', 'oxps' => 'application/oxps', 'xps' => 'application/vnd.ms-xpsdocument', // OpenOffice formats. 'odt' => 'application/vnd.oasis.opendocument.text', 'odp' => 'application/vnd.oasis.opendocument.presentation', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', 'odg' => 'application/vnd.oasis.opendocument.graphics', 'odc' => 'application/vnd.oasis.opendocument.chart', 'odb' => 'application/vnd.oasis.opendocument.database', 'odf' => 'application/vnd.oasis.opendocument.formula', // WordPerfect formats. 'wp|wpd' => 'application/wordperfect', // iWork formats. 'key' => 'application/vnd.apple.keynote', 'numbers' => 'application/vnd.apple.numbers', 'pages' => 'application/vnd.apple.pages', ) );
为了方便我上面的代码只是贴出了关键部分,大家可以看到 WordPress 默认支持上传的格式还是挺多的。
如果你有心的话,直接在这个文件里增加你自己想要支持上传的文件格式也是可以的,只需要根据上面的句式,比如 ‘png’ => ‘image/png’, 这种来写就行了。前面是支持上传的文件格式(扩展名,这里是 png),后面是这种格式的互联网媒体类型(这里是 image/png),互联网媒体类型不知道的话需要自己去查找。两外注意两边的引号也要写。
当然,一般我们还是不推荐直接去改动 WordPress 的文件的。一是会因为 WordPress 更新而需要重新更改,二也是因为这样子做不够安全,所以我们还是通过正常的途径来增加支持的文件格式比较好。
方法也比较简单,一般我们用的主题目录下都会有 functions.php 这个文件,仿照下面的代码,替换其中你要增加的文件格式,然后把代码复制到主题目录下的 functions.php 文件中就行了。(此处 PS:你要是不会操作,就直接复制到文件的最末尾去吧!)
代码如下:
function add_upload_mimes($existing_mimes = array()) { //一行一种文件格式,注意不要写错 $existing_mimes['7z'] = 'application/x-7z-compressed'; $existing_mimes['png'] = 'image/png'; //这后面的都不要改动 return $existing_mimes; } add_filter('upload_mimes', 'add_upload_mimes');
上面的代码例子是添加 7z 和 png 文件格式的上传支持,当然这两种格式默认就是支持的,请不要在意,我这里只是为了方便随便找的两种格式。
在 $existing_mimes[‘7z’] = ‘application/x-7z-compressed’; 这行代码中,你需要改变的只有前面要添加的文件格式 7z 为你自己想添加的,以及后面的 application/x-7z-compressed 为你想添加文件格式的互联网媒体类型。
前面的格式大家都懂,后面的互联网媒体类型可能一般大家都不太了解,这个直接借助搜索引擎查一查就知道,我这里就不另外说明了。
如果是无后缀该怎么添加呢?
无后缀的这个我还真没研究过,查了一下也没见到具体的办法。我觉得这种还是加后缀或者用压缩包上传会好一点吧。
谢谢
$existing_mimes['*'] = 'application/octet-stream';
好像可以这样添加,还没试不知道这样行不行
我觉得不会很靠谱,不过你可以试试。