Source
File: wp-includes/functions.php
function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
$proper_filename = false;
$wp_filetype = wp_check_filetype( $filename, $mimes );
$ext = $wp_filetype['ext'];
$type = $wp_filetype['type'];
if ( ! file_exists( $file ) ) {
return compact( 'ext', 'type', 'proper_filename' );
}
$real_mime = false;
if ( $type && 0 === strpos( $type, 'image/' ) ) {
$real_mime = wp_get_image_mime( $file );
if ( $real_mime && $real_mime != $type ) {
$mime_to_ext = apply_filters(
'getimagesize_mimes_to_exts',
array(
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/gif' => 'gif',
'image/bmp' => 'bmp',
'image/tiff' => 'tif',
)
);
if ( ! empty( $mime_to_ext[ $real_mime ] ) ) {
$filename_parts = explode( '.', $filename );
array_pop( $filename_parts );
$filename_parts[] = $mime_to_ext[ $real_mime ];
$new_filename = implode( '.', $filename_parts );
if ( $new_filename != $filename ) {
$proper_filename = $new_filename;
}
$wp_filetype = wp_check_filetype( $new_filename, $mimes );
$ext = $wp_filetype['ext'];
$type = $wp_filetype['type'];
} else {
$real_mime = false;
}
}
}
if ( $type && ! $real_mime && extension_loaded( 'fileinfo' ) ) {
$finfo = finfo_open( FILEINFO_MIME_TYPE );
$real_mime = finfo_file( $finfo, $file );
finfo_close( $finfo );
$nonspecific_types = array(
'application/octet-stream',
'application/encrypted',
'application/CDFV2-encrypted',
'application/zip',
);
if ( in_array( $real_mime, $nonspecific_types, true ) ) {
if ( ! in_array( substr( $type, 0, strcspn( $type, '/' ) ), array( 'application', 'video', 'audio' ), true ) ) {
$type = false;
$ext = false;
}
} elseif ( 0 === strpos( $real_mime, 'video/' ) || 0 === strpos( $real_mime, 'audio/' ) ) {
if ( substr( $real_mime, 0, strcspn( $real_mime, '/' ) ) !== substr( $type, 0, strcspn( $type, '/' ) ) ) {
$type = false;
$ext = false;
}
} elseif ( 'text/plain' === $real_mime ) {
if ( ! in_array(
$type,
array(
'text/plain',
'text/csv',
'text/richtext',
'text/tsv',
'text/vtt',
),
true
)
) {
$type = false;
$ext = false;
}
} elseif ( 'text/rtf' === $real_mime ) {
if ( ! in_array(
$type,
array(
'text/rtf',
'text/plain',
'application/rtf',
),
true
)
) {
$type = false;
$ext = false;
}
} else {
if ( $type !== $real_mime ) {
$type = false;
$ext = false;
}
}
}
if ( $type ) {
$allowed = get_allowed_mime_types();
if ( ! in_array( $type, $allowed, true ) ) {
$type = false;
$ext = false;
}
}
return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes, $real_mime );
}