WordPress at a glance

wp_check_filetype() WP 1.0

Retrieve the file type from the file name.

You can optionally define the mime array, if needed.

1 time — 0.0011148 sec (very slow) | 50000 times — 1.85 sec (fast) | PHP 7.3.12, WP 5.4.1

No Hooks.

Return

Array. Values for the extension and mime type.

Usage

wp_check_filetype( $filename, $mimes );
$filename(string) (required)
File name or path.
$mimes(string[])
Array of allowed mime types keyed by their file extension regex.

Changelog

Since 2.0.4 Introduced.

Code of wp_check_filetype() WP 5.7.2

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

From category: Rarely used

No comments
    Log In