wp_check_filetype

Retrieve the file type from the file name.

Description

(array) wp_check_filetype( (string) $filename, (null) $mimes = null ); 

You can optionally define the mime array, if needed.

Returns (array)

Values with extension first and mime type.

Parameters (2)

0. $filename (string)
File name or path.
1. $mimes — Optional. (null) => null
Key is the file extension with value as the mime type.

Usage

  1. if ( !function_exists( 'wp_check_filetype' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // File name or path. 
  5. $filename = ''; 
  6.  
  7. // Optional. Key is the file extension with value as the mime type. 
  8. $mimes = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_check_filetype($filename, $mimes); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function wp_check_filetype( $filename, $mimes = null ) { 
  2. if ( empty($mimes) ) 
  3. $mimes = get_allowed_mime_types(); 
  4. $type = false; 
  5. $ext = false; 
  6.  
  7. foreach ( $mimes as $ext_preg => $mime_match ) { 
  8. $ext_preg = '!\.(' . $ext_preg . ')$!i'; 
  9. if ( preg_match( $ext_preg, $filename, $ext_matches ) ) { 
  10. $type = $mime_match; 
  11. $ext = $ext_matches[1]; 
  12. break; 
  13.  
  14. return compact( 'ext', 'type' );