Home > php教程 > php手册 > PHP获取上传文件的md5码

PHP获取上传文件的md5码

WBOY
Release: 2016-06-13 09:38:14
Original
1292 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;
}
Copy after login

md5_file()

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

语法:md5(string,raw)

  • 参数string,必需。规定要计算的文件。
  • 参数charlist,可选。规定十六进制或二进制输出格式:TRUE - 原始 16 字符二进制格式;FALSE - 默认。32 字符十六进制数。
<?php
$filename = "test.txt";
$md5file = md5_file($filename);
echo $md5file;
?>
Copy after login

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

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

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

<?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

输出:

The file is ok.
Copy after login
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template