4

I updated my site to wordpress 5.0.1, but now I can not upload file .vtt anymore. This function works properly on wordpress 4.9.8

enter image description here

  • check this article - tribulant.com/blog/wordpress/… Hope it will help you – Angel Deykov Dec 13 '18 at 7:13
  • I was able to reproduce the issue on WP 5.0.1. It's worth noting that .vtt files are among the default allowed file types defined WP's mime_types filter. – Dave Romsey Dec 13 '18 at 7:42
  • 3
    This could be related to the hardening of the mime type verifications in the security update of 5.0.1 – birgire Dec 13 '18 at 8:34
3

When a file is uploaded, WordPress peeks into it to see what it's made of, to find out the real mime type. This is determined from this part in wp_check_filetype_and_ext():

$finfo = finfo_open( FILEINFO_MIME_TYPE );
$real_mime = finfo_file( $finfo, $file );

This will say that the real mime type of a .vtt file, is text/plain.

The changes in wp_check_filetype_and_ext() in 5.0.1 made the mime type validation more strict. Now it's making sure that the real mime type of the file, described by $real_mime, must match the one from the file's extension from wp_check_filetype(), if it's supported.

The reason why the .vtt upload fails in 5.0.1 is that the real mime type text/plain is not the same as text/vtt from the supported .vtt file extension.

Before 5.0.1 the output of wp_check_filetype_and_ext() when uploading a .vtt file:

Array ( [ext] => vtt [type] => text/vtt [proper_filename] => )

After 5.0.1 the output is:

Array ( [ext] => [type] => [proper_filename] => )

Using the wp_check_filetype_and_ext filter can override the logic introduced in 5.0.1 as demonstrated by Dave Romsey in the answer here.

The same seems to happen for .csv files, as reported here #45615

This should be resolved in WordPress 5.0.3 for vtt.

  • 2
    ps: I created a core ticket #45622 for the .vtt case – birgire Dec 13 '18 at 13:49
  • pps: posted a workaround here to handle multiple mime fallbacks per file extension. – birgire Dec 31 '18 at 1:08
2

I did some debugging and was able to reproduce and solve the issue, but unfortunately I was not able to figure out the specific cause. Generally speaking, it seems that .vtt files are failing the check performed by wp_check_filetype_and_ext().

I was able to upload .vtt files (Only tested on WP 5.0.1) after creating a simple plugin containing the following code adapted from this answer by brasofilo:

// Sets the extension and mime type for .vtt files.
add_filter( 'wp_check_filetype_and_ext', 'wpse_file_and_ext', 10, 4 );
function wpse_file_and_ext( $types, $file, $filename, $mimes ) {
    if ( false !== strpos( $filename, '.vtt' ) ) {
        $types['ext'] = 'vtt';
        $types['type'] = 'text/vtt';
    }

    return $types;
}

I also noted that .vtt files are already included in the allowed mimes by default by WP, so it should not be (and indeed isn't) necessary to add the vtt mime type to the list handled by the upload_mimes filter. e.g.:

add_filter( 'upload_mimes', 'wpse_mime_types');
function wpse_mime_types( $mimes ) {
  $mimes['vtt'] = 'text/vtt';
  return $mimes;
}

because the entry for vtt already exists.

I'm using a simple demonstration VTT file named test.vtt with the following contents:

WEBVTT - This file has no cues.

  • 1
    awesome, it works – Prast Dec 13 '18 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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