Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : String Functions : md5_file

md5_file

Calculates the md5 hash of a given file (PHP 4 >= 4.2.0, PHP 5, PECL hash:1.1-1.3)
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.

Parameters

filename

The filename

raw_output

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

Return Values

Returns a string on success, FALSE otherwise.

ChangeLog

Version Description
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/..')

Code Examples / Notes » md5_file

potsed

Heres a function to give an md5 for an entire directory..
function MD5_DIR($dir)
{
   if (!is_dir($dir))
   {
       return false;
   }

   $filemd5s = array();
   $d = dir($dir);
   while (false !== ($entry = $d->read()))
   {
       if ($entry != '.' && $entry != '..')
       {
            if (is_dir($dir.'/'.$entry))
            {
                $filemd5s[] = MD5_DIR($dir.'/'.$entry);
            }
            else
            {
                $filemd5s[] = md5_file($dir.'/'.$entry);
            }
        }
   }
   $d->close();
   return md5(implode('', $filemd5s));
}


richard

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


bubba

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.  ;)


Change Language


Follow Navioo On Twitter
addcslashes
addslashes
bin2hex
chop
chr
chunk_split
convert_cyr_string
convert_uudecode
convert_uuencode
count_chars
crc32
crypt
echo
explode
fprintf
get_html_translation_table
hebrev
hebrevc
html_entity_decode
htmlentities
htmlspecialchars_decode
htmlspecialchars
implode
join
levenshtein
localeconv
ltrim
md5_file
md5
metaphone
money_format
nl_langinfo
nl2br
number_format
ord
parse_str
print
printf
quoted_printable_decode
quotemeta
rtrim
setlocale
sha1_file
sha1
similar_text
soundex
sprintf
sscanf
str_getcsv
str_ireplace
str_pad
str_repeat
str_replace
str_rot13
str_shuffle
str_split
str_word_count
strcasecmp
strchr
strcmp
strcoll
strcspn
strip_tags
stripcslashes
stripos
stripslashes
stristr
strlen
strnatcasecmp
strnatcmp
strncasecmp
strncmp
strpbrk
strpos
strrchr
strrev
strripos
strrpos
strspn
strstr
strtok
strtolower
strtoupper
strtr
substr_compare
substr_count
substr_replace
substr
trim
ucfirst
ucwords
vfprintf
vprintf
vsprintf
wordwrap
eXTReMe Tracker