On this page
wp_check_filetype( string $filename, string[] $mimes = null ): array
Retrieves the file type from the file name.
Description
You can optionally define the mime array, if needed.
Parameters
$filenamestring Required-
File name or path.
$mimesstring[] Optional-
Array of allowed mime types keyed by their file extension regex.
Default:
null
Return
array Values for the extension and mime type.
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.
Source
File: wp-includes/functions.php. View all references
function wp_check_filetype( $filename, $mimes = null ) {
if ( empty( $mimes ) ) {
$mimes = get_allowed_mime_types();
}
$type = false;
$ext = false;
foreach ( $mimes as $ext_preg => $mime_match ) {
$ext_preg = '!\.(' . $ext_preg . ')$!i';
if ( preg_match( $ext_preg, $filename, $ext_matches ) ) {
$type = $mime_match;
$ext = $ext_matches[1];
break;
}
}
return compact( 'ext', 'type' );
}
Related
Uses
| Uses | Description |
|---|---|
| get_allowed_mime_types() wp-includes/functions.php | Retrieves the list of allowed mime types and file extensions. |
Used By
| Used By | Description |
|---|---|
| WP_Customize_Manager::prepare_starter_content_attachments() wp-includes/class-wp-customize-manager.php | Prepares starter content attachments. |
| get_header_video_settings() wp-includes/theme.php | Retrieves header video settings. |
| wp_attachment_is() wp-includes/post.php | Verifies an attachment is of a given type. |
| sanitize_file_name() wp-includes/formatting.php | Sanitizes a filename, replacing whitespace with dashes. |
| wp_upload_bits() wp-includes/functions.php | Creates a file in the upload folder with given content. |
| wp_check_filetype_and_ext() wp-includes/functions.php | Attempts to determine the real file type of a file. |
| wp_unique_filename() wp-includes/functions.php | Gets a filename that is sanitized and unique for the given directory. |
| wp_get_image_editor() wp-includes/media.php | Returns a WP_Image_Editor instance and loads file into it. |
| wp_video_shortcode() wp-includes/media.php | Builds the Video shortcode output. |
| wp_playlist_shortcode() wp-includes/media.php | Builds the Playlist shortcode output. |
| wp_audio_shortcode() wp-includes/media.php | Builds the Audio shortcode output. |
Changelog
| Version | Description |
|---|---|
| 2.0.4 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_check_filetype