verify_file_md5() WordPress Function
The verify_file_md5() function is used to verify the integrity of a file by checking its MD5 hash. This is useful when you want to make sure that a file has not been tampered with or corrupted.
verify_file_md5( string $filename, string $expected_md5 ) #
Calculates and compares the MD5 of a file to its expected value.
Parameters
- $filename
(string)(Required)The filename to check the MD5 of.
- $expected_md5
(string)(Required)The expected MD5 of the file, either a base64-encoded raw md5, or a hex-encoded md5.
Return
(bool|WP_Error) True on success, false when the MD5 format is unknown/unexpected, WP_Error on failure.
Source
File: wp-admin/includes/file.php
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 | function verify_file_md5( $filename , $expected_md5 ) { if ( 32 === strlen ( $expected_md5 ) ) { $expected_raw_md5 = pack( 'H*' , $expected_md5 ); } elseif ( 24 === strlen ( $expected_md5 ) ) { $expected_raw_md5 = base64_decode ( $expected_md5 ); } else { return false; // Unknown format. } $file_md5 = md5_file( $filename , true ); if ( $file_md5 === $expected_raw_md5 ) { return true; } return new WP_Error( 'md5_mismatch' , sprintf( /* translators: 1: File checksum, 2: Expected checksum value. */ __( 'The checksum of the file (%1$s) does not match the expected checksum value (%2$s).' ), bin2hex( $file_md5 ), bin2hex( $expected_raw_md5 ) ) ); } |
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
3.7.0 | Introduced. |