توابع md5_file() PHP

مثال

محاسبه هش MD5 فایل متن "test.txt":

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

The output of the above code:

d41d8cd98f00b204e9800998ecf8427e

تعریف و استفاده

توابع md5_file() هش MD5 فایل را محاسبه می‌کند.

توابع md5_file() از امنیت داده RSA استفاده می‌کند، از جمله الگوریتم خلاصه پیام MD5.

توضیحات از RFC 1321 - الگوریتم خلاصه پیام MD5: الگوریتم خلاصه پیام MD5 هر اطلاعات با طول متغیر را به یک عدد 128 بیتی تبدیل می‌کند که به عنوان "اثربخش" یا "خلاصه پیام" برای این اطلاعات استفاده می‌شود و این مقدار تبدیل شده به عنوان نتیجه استفاده می‌شود. الگوریتم MD5 به طور اصلی برای برنامه‌های امضای دیجیتال طراحی شده است؛ در این برنامه‌های امضای دیجیتال، فایل‌های بزرگ قبل از رمزنگاری (که فرآیند رمزنگاری از طریق تنظیم کلید خصوصی زیر یک سیستم رمزنگاری عمومی [مثلاً RSA] انجام می‌شود) به یک روش امن فشرده می‌شوند.

برای محاسبه هش MD5 یک رشته، از md5() Function.

Syntax

md5_file(file,raw)
Parameters Description
file Required. Specifies the file to be calculated.
raw

Optional. Boolean value, specifies the hexadecimal or binary output format:

  • TRUE - Original 16-character binary format
  • FALSE - Default. 32-character hexadecimal number

Technical Details

Return Value: If successful, returns the calculated MD5 hash; if failed, returns FALSE.
PHP Version: 4.2.0+
Update Log:

In PHP 5.0, a new raw Parameters.

Since PHP 5.1, md5_file() can be used by encapsulation. For example: md5_file("http://w3cschool.com.cn/..")

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",$md5file);
?>

Check if "test.txt" has been changed (i.e., the MD5 hash has been changed):

<?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.";
  }
?>

The output of the above code:

The file is ok.