verify_file_md5

The timeline below displays how wordpress function verify_file_md5 has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.8

/**
 * Calculates and compares the MD5 of a file to its expected value.
 *
 * @since 3.7.0
 *
 * @param string $filename     The filename to check the MD5 of.
 * @param string $expected_md5 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.
 */
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)
    ));
}

WordPress Version: 5.4

/**
 * Calculates and compares the MD5 of a file to its expected value.
 *
 * @since 3.7.0
 *
 * @param string $filename     The filename to check the MD5 of.
 * @param string $expected_md5 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.
 */
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)
    ));
}

WordPress Version: 5.3

/**
 * Calculates and compares the MD5 of a file to its expected value.
 *
 * @since 3.7.0
 *
 * @param string $filename     The filename to check the MD5 of.
 * @param string $expected_md5 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.
 */
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)
    ));
}

WordPress Version: 5.1

/**
 * Calculates and compares the MD5 of a file to its expected value.
 *
 * @since 3.7.0
 *
 * @param string $filename     The filename to check the MD5 of.
 * @param string $expected_md5 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.
 */
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(__('The checksum of the file (%1$s) does not match the expected checksum value (%2$s).'), bin2hex($file_md5), bin2hex($expected_raw_md5)));
}

WordPress Version: 3.9

/**
 * Calculates and compares the MD5 of a file to its expected value.
 *
 * @since 3.7.0
 *
 * @param string $filename The filename to check the MD5 of.
 * @param string $expected_md5 The expected MD5 of the file, either a base64 encoded raw md5, or a hex-encoded md5
 * @return bool|object WP_Error on failure, true on success, false when the MD5 format is unknown/unexpected
 */
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(__('The checksum of the file (%1$s) does not match the expected checksum value (%2$s).'), bin2hex($file_md5), bin2hex($expected_raw_md5)));
}

WordPress Version: 3.7

/**
 * Calculates and compares the MD5 of a file to it's expected value.
 *
 * @since 3.7.0
 *
 * @param string $filename The filename to check the MD5 of.
 * @param string $expected_md5 The expected MD5 of the file, either a base64 encoded raw md5, or a hex-encoded md5
 * @return bool|object WP_Error on failure, true on success, false when the MD5 format is unknown/unexpected
 */
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(__('The checksum of the file (%1$s) does not match the expected checksum value (%2$s).'), bin2hex($file_md5), bin2hex($expected_raw_md5)));
}