The check_upload_mimes() WordPress PHP function cross-verifies an array of MIME types against a predefined list of approved types. WordPress has its own set of acceptable upload file types, which is defined in the wp-includes/functions.php
file under the function get_allowed_mime_types()
. check_upload_mimes() filters this list according to the file types authorized by Multisite Super Admins at wp-admin/network/settings.php
.
Usage
Let’s take an example where you have an array of MIME types and you want to verify them against the allowed list. Here’s a brief code snippet showcasing this:
$mimes = array( 'jpg|jpeg|jpe' => 'image/jpeg', 'php' => 'application/x-php', // This isn't on the whitelist! ); $mimes = check_upload_mimes($mimes); print_r($mimes);
In this case, the output will be:
Array( 'jpg|jpeg|jpe' => 'image/jpeg' )
Parameters
- $mimes (array) (required): This is the array of MIME types you want to check.
More information
For more details, refer to the WordPress Developer Resources: check_upload_mimes()
Examples
Filtering an Array of MIME Types
If you have an array of MIME types and you want to filter them against the allowed list, you can use the check_upload_mimes() function.
// Define your MIME types array $mimes = array( 'gif' => 'image/gif', 'png' => 'image/png', 'svg' => 'image/svg+xml', 'doc' => 'application/msword', 'exe' => 'application/x-msdownload' // Not allowed! ); // Check against the allowed types $mimes = check_upload_mimes($mimes); // Print the filtered array print_r($mimes);
This will output:
Array( 'gif' => 'image/gif', 'png' => 'image/png', 'svg' => 'image/svg+xml', 'doc' => 'application/msword' )
Checking a Single MIME Type
If you only want to check a single MIME type, you can simply create an array with a single element.
// Define your MIME type $mimes = array( 'mp3' => 'audio/mpeg' ); // Check against the allowed types $mimes = check_upload_mimes($mimes); // Print the result print_r($mimes);
This will output:
Array( 'mp3' => 'audio/mpeg' )
Checking Non-Image MIME Types
You can use check_upload_mimes() to check non-image MIME types as well.
// Define your MIME types array $mimes = array( 'pdf' => 'application/pdf', 'xls' => 'application/vnd.ms-excel', 'psd' => 'image/vnd.adobe.photoshop', 'ai' => 'application/postscript' ); // Check against the allowed types $mimes = check_upload_mimes($mimes); // Print the filtered array print_r($mimes);
This will output:
Array( 'pdf' => 'application/pdf', 'xls' => 'application/vnd.ms-excel', 'psd' => 'image/vnd.adobe.photoshop', 'ai' => 'application/postscript' )
Checking Multiple Image MIME Types
You can also use check_upload_mimes() to verify multiple image MIME types.
// Define your MIME types array $mimes = array( 'jpg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'ico' => 'image/x-icon', 'bmp' => 'image/bmp', 'tiff' => 'image/tiff' ); // Check against the allowed types $mimes = check_upload_mimes($mimes); // Print the filtered array print_r($mimes);
This will output:
Array( 'jpg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'ico' => 'image/x-icon', 'bmp' => 'image/bmp', 'tiff' => 'image/tiff' )
Checking Non-Allowed MIME Types
Let’s see what happens when you check MIME types that are not on the allowed list.
// Define your MIME types array $mimes = array( 'exe' => 'application/x-msdownload', // Not allowed! 'bat' => 'application/x-msdos-program' // Not allowed! ); // Check against the allowed types $mimes = check_upload_mimes($mimes); // Print the filtered array print_r($mimes);
As expected, this will output an empty array, as none of the MIME types are on the allowed list.
Array()