|
md5_file
Calculates the md5 hash of a given file
(PHP 4 >= 4.2.0, PHP 5, PECL hash:1.1-1.3)
Code Examples / Notes » md5_filepotsed
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 Languageaddcslashes 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 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 |