wp_check_filetype_and_ext

Attempt to determine the real file type of a file.

Description

(array) wp_check_filetype_and_ext( (string) $file, (string) $filename, (null) $mimes = null ); 

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().

Returns (array)

Values for the extension, MIME, and either a corrected filename or false if original $filename is valid.

Parameters (3)

0. $file (string)
Full path to the file.
1. $filename (string)
The name of the file (may differ from $file due to $file being in a tmp directory).
2. $mimes — Optional. (null) => null
Key is the file extension with value as the mime type.

Usage

  1. if ( !function_exists( 'wp_check_filetype_and_ext' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // Full path to the file. 
  5. $file = ''; 
  6.  
  7. // The name of the file (may differ from $file due to $file being 
  8. // in a tmp directory). 
  9. $filename = ''; 
  10.  
  11. // Optional. Key is the file extension with value as the mime type. 
  12. $mimes = null; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = wp_check_filetype_and_ext($file, $filename, $mimes); 
  16.  

Defined (1)

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

/wp-includes/functions.php  
  1. function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { 
  2. $proper_filename = false; 
  3.  
  4. // Do basic extension validation and MIME mapping 
  5. $wp_filetype = wp_check_filetype( $filename, $mimes ); 
  6. $ext = $wp_filetype['ext']; 
  7. $type = $wp_filetype['type']; 
  8.  
  9. // We can't do any further validation without a file to work with 
  10. if ( ! file_exists( $file ) ) { 
  11. return compact( 'ext', 'type', 'proper_filename' ); 
  12.  
  13. $real_mime = false; 
  14.  
  15. // Validate image types. 
  16. if ( $type && 0 === strpos( $type, 'image/' ) ) { 
  17.  
  18. // Attempt to figure out what type of image it actually is 
  19. $real_mime = wp_get_image_mime( $file ); 
  20.  
  21. if ( $real_mime && $real_mime != $type ) { 
  22. /** 
  23. * Filters the list mapping image mime types to their respective extensions. 
  24. * @since 3.0.0 
  25. * @param array $mime_to_ext Array of image mime types and their matching extensions. 
  26. */ 
  27. $mime_to_ext = apply_filters( 'getimagesize_mimes_to_exts', array( 
  28. 'image/jpeg' => 'jpg',  
  29. 'image/png' => 'png',  
  30. 'image/gif' => 'gif',  
  31. 'image/bmp' => 'bmp',  
  32. 'image/tiff' => 'tif',  
  33. ) ); 
  34.  
  35. // Replace whatever is after the last period in the filename with the correct extension 
  36. if ( ! empty( $mime_to_ext[ $real_mime ] ) ) { 
  37. $filename_parts = explode( '.', $filename ); 
  38. array_pop( $filename_parts ); 
  39. $filename_parts[] = $mime_to_ext[ $real_mime ]; 
  40. $new_filename = implode( '.', $filename_parts ); 
  41.  
  42. if ( $new_filename != $filename ) { 
  43. $proper_filename = $new_filename; // Mark that it changed 
  44. // Redefine the extension / MIME 
  45. $wp_filetype = wp_check_filetype( $new_filename, $mimes ); 
  46. $ext = $wp_filetype['ext']; 
  47. $type = $wp_filetype['type']; 
  48. } else { 
  49. // Reset $real_mime and try validating again. 
  50. $real_mime = false; 
  51.  
  52. // Validate files that didn't get validated during previous checks. 
  53. if ( $type && ! $real_mime && extension_loaded( 'fileinfo' ) ) { 
  54. $finfo = finfo_open( FILEINFO_MIME_TYPE ); 
  55. $real_mime = finfo_file( $finfo, $file ); 
  56. finfo_close( $finfo ); 
  57.  
  58. /** 
  59. * If $real_mime doesn't match what we're expecting, we need to do some extra 
  60. * vetting of application mime types to make sure this type of file is allowed. 
  61. * Other mime types are assumed to be safe, but should be considered unverified. 
  62. */ 
  63. if ( $real_mime && ( $real_mime !== $type ) && ( 0 === strpos( $real_mime, 'application' ) ) ) { 
  64. $allowed = get_allowed_mime_types(); 
  65.  
  66. if ( ! in_array( $real_mime, $allowed ) ) { 
  67. $type = $ext = false; 
  68.  
  69. /** 
  70. * Filters the "real" file type of the given file. 
  71. * @since 3.0.0 
  72. * @param array $wp_check_filetype_and_ext File data array containing 'ext', 'type', and 
  73. * 'proper_filename' keys. 
  74. * @param string $file Full path to the file. 
  75. * @param string $filename The name of the file (may differ from $file due to 
  76. * $file being in a tmp directory). 
  77. * @param array $mimes Key is the file extension with value as the mime type. 
  78. */ 
  79. return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes );