Home » Functions » wp_check_filetype

Function Name: wp_check_filetype

In WordPress, the wp_check_filetype function is used to validate and determine the file type of a given file. It checks the file extension and the content of the file to accurately identify its type.

This function is particularly useful when handling file uploads in WordPress. It ensures that only valid file types are accepted and processed, preventing potential security risks from malicious files.

The wp_check_filetype function returns an associative array containing the file type and the corresponding mime type. This information can be used to perform various operations based on the file type, such as displaying appropriate icons or handling file-specific functionality.

Here’s an example usage code:

$file_path = 'path/to/file.jpg';
$file_info = wp_check_filetype( $file_path );

if ( $file_info['ext'] === 'jpg' || $file_info['ext'] === 'jpeg' ) {
    // Perform operations specific to JPEG files
} elseif ( $file_info['ext'] === 'png' ) {
    // Perform operations specific to PNG files
} elseif ( $file_info['ext'] === 'pdf' ) {
    // Perform operations specific to PDF files
} else {
    // Handle unsupported file types

In this example, we have a file with the path ‘path/to/file.jpg’. We use the wp_check_filetype function to validate the file and retrieve its file type information. Based on the file extension returned in the $file_info array, we can then perform specific operations tailored to different file types.

By utilizing the wp_check_filetype function, WordPress developers can ensure the security and reliability of file uploads while providing enhanced functionality based on the detected file types.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.