wppaste
WordPress

wp_check_filetype_and_ext( string $file, string $filename, string[]|null $mimes = null ): array

Since
3.0.0
Source
wp-includes/functions.php:3124
Attempts to determine the real file type of a file.

Description

If unable to, the file name extension will be used to determine type.

If it's determined that the extension does not match the file's real type, then the "proper_filename" value will be set with a proper filename and extension.

Currently this function only supports renaming images validated via wp_get_image_mime().

Compatibility

WordPress
since 3.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$filestring
Full path to the file.
$filenamestring
The name of the file (may differ from $file due to $file being in a tmp directory).
$mimesstring[]|nulloptional
Array of allowed mime types keyed by their file extension regex.
Defaults to the result of get_allowed_mime_types().Default: null

Return value

array
Values for the extension, mime type, and corrected filename.
  • $extstring|false

    File extension, or false if the file doesn't match a mime type.
  • $typestring|false

    File mime type, or false if the file doesn't match a mime type.
  • $proper_filenamestring|false

    File name with its correct extension, or false if it cannot be determined.

Performance profile

How much work a call to wp_check_filetype_and_ext() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads or writes the filesystem via file_exists().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
23–128

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 179.

Plugin surface
2 hooks

Third-party callbacks on 'getimagesize_mimes_to_exts', 'wp_check_filetype_and_ext' run inside this call, and their cost is not bounded by anything here.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfile_exists()called directly

Further down the call graph this can also reach query. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 33 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_check_filetype_and_ext() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!file_exists()23wp_check_filetype(), file_exists(), compact()
file_exists()33–38wp_check_filetype(), file_exists(), compact(), apply_filters()
file_exists() && $type42–49wp_check_filetype(), file_exists(), wp_get_image_mime(), compact(), apply_filters()
file_exists() && !$type44–46wp_check_filetype(), file_exists(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && $type50–57wp_check_filetype(), file_exists(), wp_get_image_mime(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && $type && empty($mime_to_ext[$real_mime])52–57wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), compact(), apply_filters()
file_exists() && $type && empty($mime_to_ext[$real_mime])60–65wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && !$type && !$real_mime62–70wp_check_filetype(), file_exists(), finfo_open(), finfo_file(), compact(), apply_filters()
file_exists() && !$type && $real_mime62–66wp_check_filetype(), file_exists(), finfo_open(), finfo_file(), strcspn(), compact(), apply_filters()
file_exists() && !$type && !$real_mime68–78wp_check_filetype(), file_exists(), finfo_open(), finfo_file(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && $type && !$real_mime68–81wp_check_filetype(), file_exists(), wp_get_image_mime(), finfo_open(), finfo_file(), compact(), apply_filters()
file_exists() && !$type && $real_mime68–74wp_check_filetype(), file_exists(), finfo_open(), finfo_file(), strcspn(), get_allowed_mime_types(), compact(), apply_filters()
21 further outcomes, up to 128 instructions
file_exists() && $type && $real_mime68–77wp_check_filetype(), file_exists(), wp_get_image_mime(), finfo_open(), finfo_file(), strcspn(), compact(), apply_filters()
file_exists() && !$type70–76wp_check_filetype(), file_exists(), finfo_open(), finfo_file(), strcspn(), strcspn(), compact(), apply_filters()
file_exists() && $type && !$real_mime74–89wp_check_filetype(), file_exists(), wp_get_image_mime(), finfo_open(), finfo_file(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && $type && $real_mime74–85wp_check_filetype(), file_exists(), wp_get_image_mime(), finfo_open(), finfo_file(), strcspn(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && !$type76–84wp_check_filetype(), file_exists(), finfo_open(), finfo_file(), strcspn(), strcspn(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && $type76–87wp_check_filetype(), file_exists(), wp_get_image_mime(), finfo_open(), finfo_file(), strcspn(), strcspn(), compact(), apply_filters()
file_exists() && $type && !empty($mime_to_ext[$real_mime])76–82wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), explode(), array_pop(), wp_check_filetype(), compact(), apply_filters()
file_exists() && $type && empty($mime_to_ext[$real_mime]) && !$real_mime78–89wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), finfo_open(), finfo_file(), compact(), apply_filters()
file_exists() && $type && empty($mime_to_ext[$real_mime]) && $real_mime78–85wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), finfo_open(), finfo_file(), strcspn(), compact(), apply_filters()
file_exists() && $type82–95wp_check_filetype(), file_exists(), wp_get_image_mime(), finfo_open(), finfo_file(), strcspn(), strcspn(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && $type && empty($mime_to_ext[$real_mime]) && !$real_mime84–97wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), finfo_open(), finfo_file(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && $type && empty($mime_to_ext[$real_mime]) && $real_mime84–93wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), finfo_open(), finfo_file(), strcspn(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && $type && !empty($mime_to_ext[$real_mime])84–90wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), explode(), array_pop(), wp_check_filetype(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && $type && empty($mime_to_ext[$real_mime])86–95wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), finfo_open(), finfo_file(), strcspn(), strcspn(), compact(), apply_filters()
file_exists() && $type && empty($mime_to_ext[$real_mime])92–103wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), finfo_open(), finfo_file(), strcspn(), strcspn(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && $type && !empty($mime_to_ext[$real_mime]) && !$real_mime102–114wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), explode(), array_pop(), wp_check_filetype(), finfo_open(), finfo_file(), compact(), apply_filters()
file_exists() && $type && !empty($mime_to_ext[$real_mime]) && $real_mime102–110wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), explode(), array_pop(), wp_check_filetype(), finfo_open(), finfo_file(), strcspn(), compact(), apply_filters()
file_exists() && $type && !empty($mime_to_ext[$real_mime]) && !$real_mime108–122wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), explode(), array_pop(), wp_check_filetype(), finfo_open(), finfo_file(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && $type && !empty($mime_to_ext[$real_mime]) && $real_mime108–118wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), explode(), array_pop(), wp_check_filetype(), finfo_open(), finfo_file(), strcspn(), get_allowed_mime_types(), compact(), apply_filters()
file_exists() && $type && !empty($mime_to_ext[$real_mime])110–120wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), explode(), array_pop(), wp_check_filetype(), finfo_open(), finfo_file(), strcspn(), strcspn(), compact(), apply_filters()
file_exists() && $type && !empty($mime_to_ext[$real_mime])116–128wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), explode(), array_pop(), wp_check_filetype(), finfo_open(), finfo_file(), strcspn(), strcspn(), get_allowed_mime_types(), compact(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev17923–12827
8.517923–12827
8.417923–1282730 fewer instructions than PHP 8.3
8.320923–15527
8.220923–15527
8.120923–155273 fewer instructions than PHP 7.4
7.421223–15827

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_check_filetype_and_ext() runs, in this order:

  1. apply_filters( getimagesize_mimes_to_exts )filterline 3159 (+35 into the body)

    Filters the list mapping image mime types to their respective extensions.

  2. apply_filters( wp_check_filetype_and_ext )filterline 3352 (+228 into the body)

    Filters the "real" file type of the given file.

Uses · 5

Used by · 4

Source code

function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {	$proper_filename = false; 	// Do basic extension validation and MIME mapping.	$wp_filetype = wp_check_filetype( $filename, $mimes );	$ext         = $wp_filetype['ext'];	$type        = $wp_filetype['type']; 	// We can't do any further validation without a file to work with.	if ( ! file_exists( $file ) ) {		return compact( 'ext', 'type', 'proper_filename' );	} 	$real_mime = false; 	// Validate image types.	if ( $type && str_starts_with( $type, 'image/' ) ) { 		// Attempt to figure out what type of image it actually is.		$real_mime = wp_get_image_mime( $file ); 		$heic_images_extensions = array(			'heif',			'heics',			'heifs',		); 		if ( $real_mime && ( $real_mime !== $type || in_array( $ext, $heic_images_extensions, true ) ) ) {			/**			 * Filters the list mapping image mime types to their respective extensions.			 *			 * @since 3.0.0			 *			 * @param array $mime_to_ext Array of image mime types and their matching extensions.			 */			$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',					'image/webp'          => 'webp',					'image/avif'          => 'avif', 					/*					 * In theory there are/should be file extensions that correspond to the					 * mime types: .heif, .heics and .heifs. However it seems that HEIC images					 * with any of the mime types commonly have a .heic file extension.					 * Seems keeping the status quo here is best for compatibility.					 */					'image/heic'          => 'heic',					'image/heif'          => 'heic',					'image/heic-sequence' => 'heic',					'image/heif-sequence' => 'heic',				)			); 			// Replace whatever is after the last period in the filename with the correct extension.			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; // Mark that it changed.				} 				// Redefine the extension / MIME.				$wp_filetype = wp_check_filetype( $new_filename, $mimes );				$ext         = $wp_filetype['ext'];				$type        = $wp_filetype['type'];			} else {				// Reset $real_mime and try validating again.				$real_mime = false;			}		}

Changelog

Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.