md5_file

(PHP 4 >= 4.2.0, PHP 5)

md5_file -- Calculates the md5 hash of a given file

说明

string md5_file ( string filename [, bool raw_output] )

Calculates the MD5 hash of the file specified by the filename parameter using the RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash. The hash is a 32-character hexadecimal number.

参数

filename

The filename

raw_output

When TRUE, returns the digest in raw binary format with a length of 16. Defaults to FALSE.

返回值

Returns a string on success, FALSE otherwise.

更新日志

版本说明
5.0.0 Added the raw_output parameter
5.1.0 Changed the function to use the streams API. It means that you can use it with wrappers, like md5_file('http://example.com/..')


add a note add a note User Contributed Notes
bubba at revbubba dot com
29-Mar-2005 08:56
a working example of the usage of this function, to confirm a specific file has not been modified (replace all instances of "myfile.xxx" with your filename):

<?php
$chkfilename
= "myfile.xxx";
$chkmd5return = md5_file($chkfilename);
if (
$chkmd5return != "myfile.xxx's md5 value") {
     echo
"You have replaced myfile.xxx with an unknown version of the file, please replace the original file.";
} else {
     (
your code to be executed, now that it has confirmed your myfile.xxx has been unmodified)
}
?>

To find out the file's md5 value, create a new .php doc, and put this code in it:

<?php
$chkfilename
= "myfile.xxx";
$chkmd5return = md5_file($chkfilename);
echo
$chkmd5return;
?>

Then upload the new .php doc to your webserver and navigate to it.  Be sure to delete the new .php doc once you have plugged in the value it spits out, into the "myfile.xxx's md5 value" in the first example above.

I just thought this example might be helpful to someone somewhere...  if you php.net people feel it needs editing or deletion, I leave it to your discretion.  ;)
richard at interlink dot com dot au
16-Nov-2004 04:57
For those of you with PHP 4 that want to output the "raw" 128 bit hash, all you need to do is send it to pack to convert the hex string into the raw output.

ie:
$filename="checkthisfile.bin";
$rawhash=pack("H*",md5_file($filename));