PHP gets the MD5 value of the file and determines whether it is modified by the example _php tips

Source: Internet
Author: User
Tags hash md5 md5 encryption md5 hash

Due to the need to determine whether the uploaded file has been modified, you need to record the MD5 value of the uploaded file, here to record the way to get the file MD5 value.

Copy Code code as follows:

if (Isset ($_files[' Multimedia ')) && $_files[' multimedia '] [' ERROR ']==0]
{
$file _name = $_files[' multimedia ' [' Name '];
$size = getimagesize ($_files[' multimedia '] [' tmp_name ']);
$type = $_files[' multimedia '] [' type '];
$original = $_files[' multimedia '] [' tmp_name '];
$MD 5 = md5_file ($original);
echo $MD 5;
}

Md5_file ()

The Md5_file () function computes the MD5 hash of the file. The MD5 () function uses RSA data security, including the MD5 message selected passage algorithm. If successful, returns the computed MD5 hash, or False if it fails.

Syntax: MD5 (STRING,RAW)

Parameter string, required. Specify the files to be computed.

Parameter charlist, optional. Specify hexadecimal or binary output format: TRUE-original 16 character binary format; FALSE-Default. 32-character hexadecimal number.

Copy Code code as follows:

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

To store the MD5 hash of the "test.txt" file:

Copy Code code as follows:

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

In this case, we will detect if "test.txt" has been changed (that is, whether the hash has been changed): MD5

Copy Code code as follows:

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

Output:

Copy Code code as follows:

The file is OK.

PS: Here again for you to provide 2 MD5 encryption tools, interested friends can refer to:

MD5 Online Encryption Tool:

Http://tools.jb51.net/password/CreateMD5Password

Online md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160 Encryption Tool:

Http://tools.jb51.net/password/hash_md5_sha

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.