4
votes

I'm trying to upload .kml files to WordPress. I had this working at one point but the latest WordPress update seems to have broken it.

I'm using this function

function my_myme_types($mime_types){
$mime_types['kml'] = 'application/vnd.google-earth.kml+xml'; //Adding kml extension
$mime_types['kmz'] = 'application/vnd.google-earth.kmz'; //Adding kmz files
return $mime_types;
}
add_filter('upload_mimes', 'my_myme_types', 1, 1);

I get this error when uploading

"Sorry, this file type is not permitted for security reasons."

2

2 Answers

13
votes

For KML/KMZ files to be properly supported, you'll have to use text/xml and application/zip instead, because WordPress compares the declared MIME type to the 'real' detected MIME type (see function wp_check_filetype_and_ext in wp-includes/functions.php for more details)

function add_upload_mimes($mimes) {
  $mimes['kml'] = 'text/xml';
  $mimes['kmz'] = 'application/zip';
  return $mimes;
}
add_filter('upload_mimes', 'add_upload_mimes');

Update (2019-02-28) : kml is detected as text/xml and not application/xml, changing the code accordingly should resolve the issue described in the comments below.

1
votes

This may be related as of after the latest wordpress update:

Trying to upload xml file and getting the same error message, updating upload_mimes WordPress filter hook doesn't work as well as using some file uploading or mime type managing plugins which essentially using the same filter hook.

Solution: update wp-config.php and add in the following line

define( 'ALLOW_UNFILTERED_UPLOADS', true );

and then remove this line after uploading the files to avoid potential security risk