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. // Validate image types. 
  14. if ( $type && 0 === strpos( $type, 'image/' ) ) { 
  15.  
  16. // Attempt to figure out what type of image it actually is 
  17. $real_mime = wp_get_image_mime( $file ); 
  18.  
  19. if ( ! $real_mime ) { 
  20. $type = $ext = false; 
  21. } elseif ( $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. $type = $ext = false; 
  50. } elseif ( function_exists( 'finfo_file' ) ) { 
  51. // Use finfo_file if available to validate non-image files. 
  52. $finfo = finfo_open( FILEINFO_MIME_TYPE ); 
  53. $real_mime = finfo_file( $finfo, $file ); 
  54. finfo_close( $finfo ); 
  55.  
  56. // If the extension does not match the file's real type, return false. 
  57. if ( $real_mime !== $type ) { 
  58. $type = $ext = false; 
  59.  
  60. /** 
  61. * Filters the "real" file type of the given file. 
  62. * @since 3.0.0 
  63. * @param array $wp_check_filetype_and_ext File data array containing 'ext', 'type', and 
  64. * 'proper_filename' keys. 
  65. * @param string $file Full path to the file. 
  66. * @param string $filename The name of the file (may differ from $file due to 
  67. * $file being in a tmp directory). 
  68. * @param array $mimes Key is the file extension with value as the mime type. 
  69. */ 
  70. return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes );