verify_file_md5

函式
verify_file_md5 ( $filename, $expected_md5 )
引數
  • (string) $filename The filename to check the MD5 of.
    Required:
  • (string) $expected_md5 The expected MD5 of the file, either a base64-encoded raw md5, or a hex-encoded md5.
    Required:
返回值
  • (bool|WP_Error) True on success, false when the MD5 format is unknown/unexpected, WP_Error on failure.
定義位置
相關方法
verify_file_signatureget_file_datawp_verify_nonceget_filesystem_methodget_real_file_to_edit
引入
3.7.0
棄用
-

verify_file_md5: 這個WordPress函式用來驗證一個檔案的MD5雜湊值。它檢查檔案的MD5雜湊值是否與預期值相符,如果雜湊值有效則返回真,否則返回假。

計算並比較一個檔案的MD5和它的預期值。

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 )
		)
	);
}

常見問題

FAQs
檢視更多 >