verify_file_md5
Calculates and compares the MD5 of a file to its expected value.
Description
Returns (bool|object)
WP_Error on failure, true on success, false when the MD5 format is unknown/unexpected
Parameters (2)
- 0. $filename (string)
- The filename to check the MD5 of.
- 1. $expected_md5 (string)
- The expected MD5 of the file, either a base64 encoded raw md5, or a hex-encoded md5
Usage
if ( !function_exists( 'verify_file_md5' ) ) { require_once ABSPATH . '/wp-admin/includes/file.php'; } // The filename to check the MD5 of. $filename = ''; // The expected MD5 of the file, either a base64 encoded raw md5, or a hex-encoded md5 $expected_md5 = ''; // NOTICE! Understand what this does before running. $result = verify_file_md5($filename, $expected_md5);
Defined (1)
The function is defined in the following location(s).
- /wp-admin/includes/file.php
- 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;
- }