Home > php教程 > php手册 > body text

PHP获取文件的MD5值并判断是否被修改的例子

WBOY
Release: 2016-06-13 09:30:15
Original
892 people have browsed it

由于需要判断上传的文件是否被修改过,需要记录上传文件的md5值,这里记录一下获取文件md5值的方法。

复制代码 代码如下:


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

md5_file()

md5_file() 函数计算文件的 MD5 散列。md5() 函数使用 RSA 数据安全,包括 MD5 报文摘译算法。如果成功,则返回所计算的 MD5 散列,如果失败,则返回 false。

语法:md5(string,raw)

参数string,必需。规定要计算的文件。

参数charlist,可选。规定十六进制或二进制输出格式:TRUE - 原始 16 字符二进制格式;FALSE - 默认。32 字符十六进制数。

复制代码 代码如下:


$filename = "test.txt";
$md5file = md5_file($filename);
echo $md5file;
?>

存储 "test.txt" 文件的 MD5 散列:

复制代码 代码如下:


$md5file = md5_file("test.txt");
file_put_contents("md5file.txt",$md5file);
?>

在本例中,我们将检测 "test.txt" 是否已被更改(即是否 MD5 散列已被更改):

复制代码 代码如下:


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

输出:

复制代码 代码如下:


The file is ok.

Related labels:
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 Recommendations
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!