0

I am getting the following error when trying to upload a xls file.

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

Tried plugin wp-extra file type and there is no option to choose an xls file type.

tried pro mime type plugin, there is an option for Xls to allow and I did it too but still not working and getting the same error.

Also tried to add the following code in a function.php

function my_myme_types($mime_types){
      // New allowed mime types.
$mime_types['xls'] = 'application/vnd.ms-excel';

return $mime_types;
}
add_filter('upload_mimes', 'my_myme_types', 1, 1);

but still showing the same error.

Also tried

function wpse294198_mime_types( $mimes ) {
    $mimes['xls|xlsx'] = 'application/vnd.ms-excel';
    return $mimes;
}
add_filter( 'mime_types', 'wpse294198_mime_types' );

the only solution worked is wp-config ALLOW UNFILTERED UPLOADS. but this is risky if we considered the security reason. I just want to allow xls file not any other file format like Php, js extra.

 define(‘ALLOW_UNFILTERED_UPLOADS’, true)

the problem I face with the above solution that now anyone can upload any type of file from anywhere if there is upload option for him. So I believe this is not a good thing. I need a better solution to my problem.

I am using WordPress v5.2 and someone suggested me to try below solution but didn't work either.

function wpse323750_multi_mimes( $check, $file, $filename, $mimes ) {
  if ( empty( $check['ext'] ) && empty( $check['type'] ) ) {
    $multi_mimes = [ [ 'xls' => 'application/excel' ], [ 'xls' => 'application/vnd.ms-excel' ], ['xls' => 'application/x-excel' ], ['xls' => 'application/x-msexcel'] ];

  foreach( $multi_mimes as $mime ) {
    remove_filter( 'wp_check_filetype_and_ext', 'wpse323750_multi_mimes', 99, 4 );
    $check = wp_check_filetype_and_ext( $file, $filename, $mime );
    add_filter( 'wp_check_filetype_and_ext', 'wpse323750_multi_mimes', 99, 4 );
    if ( ! empty( $check['ext'] ) ||  ! empty( $check['type'] ) ) {
      return $check;
    }
  }
  }
  return $check;
}
add_filter( 'wp_check_filetype_and_ext', 'wpse323750_multi_mimes', 99, 4 );
3
  • When you say "Also tried to add the following code in a function.php", was the file named function.php or functions.php? That will only work if you insert the code into your active theme's functions.php file. function.php would be ignored.
    – Pat J
    Jul 26, 2019 at 15:42
  • 1
    Ask your hoster if server allows XLS files to be uploaded.
    – Max Yudin
    Jul 26, 2019 at 18:09
  • this answer solved my problem... stackoverflow.com/questions/57219470/… Jul 29, 2019 at 11:00

1 Answer 1

0

Try the same function but different $mime_types key ('xla|xls|xlt|xlw'):

<?php
function my_myme_types( $mime_types ){
    // New allowed mime types.
    $mime_types['xla|xls|xlt|xlw'] = 'application/vnd.ms-excel';

    return $mime_types;
}
add_filter( 'upload_mimes', 'my_myme_types', 1, 1 );

To get MIME type array keys and values used by WordPress:

<?php
$mimes = wp_get_mime_types();

echo '<pre>';
print_r( $data );
echo '</pre>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.