wp_check_filetype_and_ext( string $file, string $filename, string[]|null $mimes = null ): array
- Since
- 3.0.0
- Source
wp-includes/functions.php:3124
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|falseFile extension, or false if the file doesn't match a mime type.$typestring|falseFile mime type, or false if the file doesn't match a mime type.$proper_filenamestring|falseFile 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
- Scaling
- Scales with input
- Instructions
- 23–128
- Plugin surface
- 2 hooks
- Called by
- 4
Reads or writes the filesystem via file_exists().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 179.
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.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
file_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.
| When | Instructions | Calls it makes |
|---|---|---|
!file_exists() | 23 | wp_check_filetype(), file_exists(), compact() |
file_exists() | 33–38 | wp_check_filetype(), file_exists(), compact(), apply_filters() |
file_exists() && $type | 42–49 | wp_check_filetype(), file_exists(), wp_get_image_mime(), compact(), apply_filters() |
file_exists() && !$type | 44–46 | wp_check_filetype(), file_exists(), get_allowed_mime_types(), compact(), apply_filters() |
file_exists() && $type | 50–57 | wp_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–57 | wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), compact(), apply_filters() |
file_exists() && $type && empty($mime_to_ext[$real_mime]) | 60–65 | wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), get_allowed_mime_types(), compact(), apply_filters() |
file_exists() && !$type && !$real_mime | 62–70 | wp_check_filetype(), file_exists(), finfo_open(), finfo_file(), compact(), apply_filters() |
file_exists() && !$type && $real_mime | 62–66 | wp_check_filetype(), file_exists(), finfo_open(), finfo_file(), strcspn(), compact(), apply_filters() |
file_exists() && !$type && !$real_mime | 68–78 | wp_check_filetype(), file_exists(), finfo_open(), finfo_file(), get_allowed_mime_types(), compact(), apply_filters() |
file_exists() && $type && !$real_mime | 68–81 | wp_check_filetype(), file_exists(), wp_get_image_mime(), finfo_open(), finfo_file(), compact(), apply_filters() |
file_exists() && !$type && $real_mime | 68–74 | wp_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_mime | 68–77 | wp_check_filetype(), file_exists(), wp_get_image_mime(), finfo_open(), finfo_file(), strcspn(), compact(), apply_filters() |
file_exists() && !$type | 70–76 | wp_check_filetype(), file_exists(), finfo_open(), finfo_file(), strcspn(), strcspn(), compact(), apply_filters() |
file_exists() && $type && !$real_mime | 74–89 | wp_check_filetype(), file_exists(), wp_get_image_mime(), finfo_open(), finfo_file(), get_allowed_mime_types(), compact(), apply_filters() |
file_exists() && $type && $real_mime | 74–85 | wp_check_filetype(), file_exists(), wp_get_image_mime(), finfo_open(), finfo_file(), strcspn(), get_allowed_mime_types(), compact(), apply_filters() |
file_exists() && !$type | 76–84 | wp_check_filetype(), file_exists(), finfo_open(), finfo_file(), strcspn(), strcspn(), get_allowed_mime_types(), compact(), apply_filters() |
file_exists() && $type | 76–87 | wp_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–82 | wp_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_mime | 78–89 | wp_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_mime | 78–85 | wp_check_filetype(), file_exists(), wp_get_image_mime(), apply_filters(), finfo_open(), finfo_file(), strcspn(), compact(), apply_filters() |
file_exists() && $type | 82–95 | wp_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_mime | 84–97 | wp_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_mime | 84–93 | wp_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–90 | wp_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–95 | wp_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–103 | wp_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_mime | 102–114 | wp_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_mime | 102–110 | wp_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_mime | 108–122 | wp_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_mime | 108–118 | wp_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–120 | wp_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–128 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 179 | 23–128 | 27 | |
| 8.5 | 179 | 23–128 | 27 | |
| 8.4 | 179 | 23–128 | 27 | 30 fewer instructions than PHP 8.3 |
| 8.3 | 209 | 23–155 | 27 | |
| 8.2 | 209 | 23–155 | 27 | |
| 8.1 | 209 | 23–155 | 27 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 212 | 23–158 | 27 |
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:
- apply_filters( getimagesize_mimes_to_exts )filterline 3159 (+35 into the body)
Filters the list mapping image mime types to their respective extensions.
- apply_filters( wp_check_filetype_and_ext )filterline 3352 (+228 into the body)
Filters the "real" file type of the given file.
Uses · 5
- wp_check_filetype()Retrieves the file type from the file name.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_get_image_mime()Returns the real mime type of an image file.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_allowed_mime_types()Retrieves the list of allowed mime types and file extensions.
Used by · 4
- Custom_Background::handle_upload()Handles an Image upload for the background image.
- Custom_Image_Header::step_2_manage_upload()Uploads the file to be cropped in the second step.
- _wp_handle_upload()Handles PHP uploads in WordPress.
- wp_ajax_upload_attachment()Handles uploading attachments via AJAX.
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.
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.