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().
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
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.
Hooks 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
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; } }History
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.