I am trying to upload java files to my WordPress blog but it does not let me upload any files with .java extension.
I get this error:
“.java” has failed to upload due to an error
File type does not meet security guidelines. Try another.
How do I add .java extension so that it allows me to upload java source code?
I am using WordPress version 3.0.4.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Use filter ‘upload_mimes‘.
<?php
add_filter('upload_mimes','add_java_files');
function add_java_files($mimes)
{
// Add file extension 'extension' with mime type 'mime/type'
$mimes['java'] = 'text/x-java-source';
return $mimes;
}
Method 2
I found this nice function that does the trick
<?php
function addUploadMimes($mimes) {
$mimes = array_merge($mimes, array(
'tmbundle|tmCommand|tmDragCommand|tmSnippet|tmLanguage|tmPreferences' => 'application/octet-stream'
));
return $mimes;
}
add_filter('upload_mimes', 'addUploadMimes');
?>
you can add more file types by adding them on "'tmbundle|tmCommand|tmDragCommand|tmSnippet|tmLanguage|tmPreferences'"
separated by a pipe (|)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0