PHP computed file MD5 hash function md5_file ()

Source: Internet
Author: User
Tags md5 digest md5 hash

Instance

Computes the MD5 hash of the text file "Test.txt":

<?php$filename = "Test.txt"; $md 5file = Md5_file ($filename); Echo $md 5file;? >

The above code will output:

d41d8cd98f00b204e9800998ecf8427e

Definition and usage

The Md5_file () function computes the MD5 hash of the file.

The Md5_file () function uses RSA data security, including MD5 Digest to algorithm.

Explanation from RFC 1321-MD5 Digest to algorithm: MD5 Digest to the algorithm will be arbitrary length of information as input value, and convert it into a 128-bit length of "fingerprint Information" or "Digest to" value to represent this input value, and with the converted value as the result. The MD5 algorithm is designed primarily for digitally signed applications, where large files are compressed in a secure manner prior to encryption (where encryption is done by setting a private key under the public key of a cryptographic system, such as RSA).

To calculate the MD5 hash of a string, use the MD5 () function.

Grammar

Md5_file (File,raw)

Parameter description

File required. Specify the documents to be calculated.

Raw is optional. A Boolean value that specifies the hexadecimal or binary output format:

TRUE-Original 16 character binary format

FALSE-Default. 32-character hexadecimal number

Technical details

Return value: Returns the computed MD5 hash if successful, or FALSE if it fails.

PHP version: 4.2.0+

Update log: In PHP 5.0, the raw parameter becomes optional.

From PHP 5.1, md5_file () can be used through encapsulation. Example: Md5_file ("http://w3cschool.cc/..")

More examples

Example 1

Store the MD5 hash of "test.txt" in the file:

<?php$md5file = Md5_file ("test.txt"); File_put_contents ("Md5file.txt", $md 5file);? >

Detects if "Test.txt" has been changed (that is, if the MD5 hash has been changed):

<?php$md5file = file_get_contents ("Md5file.txt"), if (Md5_file ("test.txt") = = $md 5file) {echo "The file is OK."} Else{echo "The file has been changed.";}? >

The above code will output:

The file is OK.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.