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 validating images known to getimagesize().

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. // We're able to validate images using GD 
  14. if ( $type && 0 === strpos( $type, 'image/' ) && function_exists('getimagesize') ) { 
  15.  
  16. // Attempt to figure out what type of image it actually is 
  17. $imgstats = @getimagesize( $file ); 
  18.  
  19. // If getimagesize() knows what kind of image it really is and if the real MIME doesn't match the claimed MIME 
  20. if ( !empty($imgstats['mime']) && $imgstats['mime'] != $type ) { 
  21. /** 
  22. * Filters the list mapping image mime types to their respective extensions. 
  23. * @since 3.0.0 
  24. * @param array $mime_to_ext Array of image mime types and their matching extensions. 
  25. */ 
  26. $mime_to_ext = apply_filters( 'getimagesize_mimes_to_exts', array( 
  27. 'image/jpeg' => 'jpg',  
  28. 'image/png' => 'png',  
  29. 'image/gif' => 'gif',  
  30. 'image/bmp' => 'bmp',  
  31. 'image/tiff' => 'tif',  
  32. ) ); 
  33.  
  34. // Replace whatever is after the last period in the filename with the correct extension 
  35. if ( ! empty( $mime_to_ext[ $imgstats['mime'] ] ) ) { 
  36. $filename_parts = explode( '.', $filename ); 
  37. array_pop( $filename_parts ); 
  38. $filename_parts[] = $mime_to_ext[ $imgstats['mime'] ]; 
  39. $new_filename = implode( '.', $filename_parts ); 
  40.  
  41. if ( $new_filename != $filename ) { 
  42. $proper_filename = $new_filename; // Mark that it changed 
  43. // Redefine the extension / MIME 
  44. $wp_filetype = wp_check_filetype( $new_filename, $mimes ); 
  45. $ext = $wp_filetype['ext']; 
  46. $type = $wp_filetype['type']; 
  47.  
  48. /** 
  49. * Filters the "real" file type of the given file. 
  50. * @since 3.0.0 
  51. * @param array $wp_check_filetype_and_ext File data array containing 'ext', 'type', and 
  52. * 'proper_filename' keys. 
  53. * @param string $file Full path to the file. 
  54. * @param string $filename The name of the file (may differ from $file due to 
  55. * $file being in a tmp directory). 
  56. * @param array $mimes Key is the file extension with value as the mime type. 
  57. */ 
  58. return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes );