wp_check_filetype() WordPress Function

The wp_check_filetype() function is used to check the file type of a given file. This function returns the file type in the form of a string. The function is useful for verifying the file type before performing any operations on the file.

wp_check_filetype( string $filename, string[] $mimes = null ) #

Retrieve the file type from the file name.


Description

You can optionally define the mime array, if needed.


Top ↑

Parameters

$filename

(string)(Required)File name or path.

$mimes

(string[])(Optional) Array of allowed mime types keyed by their file extension regex.

Default value: null


Top ↑

Return

(array) Values for the extension and mime type.

  • 'ext'
    (string|false) File extension, or false if the file doesn't match a mime type.
  • 'type'
    (string|false) File mime type, or false if the file doesn't match a mime type.


Top ↑

Source

File: wp-includes/functions.php

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' );
}


Top ↑

Changelog

Changelog
VersionDescription
2.0.4Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More