Example of PHP getting the MD5 value of a file and determining whether it has been modified

怪我咯
Release: 2023-03-09 08:30:02
Original
2480 people have browsed it

This article mainly introduces an example of PHP obtaining the MD5 value of a file and determining whether it has been modified. Message Digest Algorithm MD5 (Chinese name is Message Digest Algorithm Version 5) is a hash function widely used in the field of computer security. , used to provide message integrity protection. Friends who need it can refer to

Since it is necessary to determine whether the uploaded file has been modified, it is necessary to record the md5 value of the uploaded file. Here is the method of obtaining the md5 value of the file. .

The code is 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'];
 $md5 = md5_file($original);
 echo $md5;
}
Copy after login

md5_file()

md5_file() function calculates the MD5 hash of the file . The md5() function uses RSA data security, including the MD5 message digest algorithm. Returns the calculated MD5 hash on success, false on failure.

Syntax: md5(string,raw)

Parameter string, required. Specifies the file to be calculated.

Parameter charlist, optional. Specifies hexadecimal or binary output format: TRUE - raw 16-character binary format; FALSE - default. 32-character hexadecimal number.

The code is as follows:

<?php
$filename = "test.txt";
$md5file = md5_file($filename);
echo $md5file;
?>
Copy after login

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

The code is as follows:

<?php
$md5file = md5_file("test.txt");
file_put_contents("md5file.txt",$md5file);
?>
Copy after login

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

Code As follows:

<?php
$md5file = file_get_contents("md5file.txt");
if (md5_file("test.txt") == $md5file)
{
 echo "The file is ok.";
}
else
{
 echo "The file has been changed.";
}
?>
Copy after login

Output:

The code is as follows:

The file is ok.
Copy after login

The above is the detailed content of Example of PHP getting the MD5 value of a file and determining whether it has been modified. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!