计算并比较文件的MD5与其预期值。
原型
verify_file_md5( string $filename, string $expected_md5 )
参数
$filename
(string)
(Required)
用于检查MD5的文件名。
$expected_md5
(string)
(Required)
文件的预期MD5,可以是base64编码的原始md5,也可以是十六进制编码的md5
返回值
(bool|object)
源文件
路径:wp-admin/includes/file.php
<?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;
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 ) ) );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/verify_file_md5/