How to allow .xls file upload?

I am getting following error while trying to upload xls file.

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

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

Tried Pro MimeType Plugin, there is an option to allow xls and i did that too but still not working and getting the same error.

Add the following code to a . also tried to add 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.

even 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 that worked is wp-config ALLOW UNFILTERED UPLOADS. But if we consider the security reasons then it is risky. I just want to allow xls file not any other file format like php, js extra.

 define(‘ALLOW_UNFILTERED_UPLOADS’, true)

The problem I am facing with the above solution is now one can upload any type of file from anywhere if there is an upload option for that. So I believe it is not a good thing. I want better solution to my problem.

I am using wordpress v5.2 and someone suggested me to try below solution but didn’t work.

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 );

Leave a Comment