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));
}
md5_file
(PHP 4 >= 4.2.0, PHP 5)
md5_file — Berechnet den MD5-Code einer Datei
Beschreibung
string md5_file
( string
$filename
[, bool $raw_output = false
] )
Berechnet den MD5-Code einer Datei, deren Dateiname mit
filename übergeben wurde, gemäß des
» RSA Data Security, Inc.
MD5 Message-Digest Algorithm, und liefert das Ergebnis zurück.
Der Code ist eine 32 Zeichen lange Hexadezimalnummer.
Parameter-Liste
-
filename -
Der Dateiname
-
raw_output -
Sofern
TRUE, wird der MD5-Wert im Raw Binary Format mit einer Länge von 16 Zeichen zurückgegeben.
Rückgabewerte
Gibt bei Erfolg einen String zurück, sonst FALSE.
Changelog
| Version | Beschreibung |
|---|---|
| 5.0.0 |
raw_output-Parameter hinzugefügt
|
| 5.1.0 | Funktionsänderung, um die Streams API nutzen zu können. Das bedeutet, Sie können die Funktion mit Wrappern wie md5_file('http://example.com/..') verwenden. |
Beispiele
Beispiel #1 Anwendungsbeispiel für md5_file()
<?php
$file = 'php-5.3.0alpha2-Win32-VC9-x64.zip';
echo 'MD5-Dateihash von ' . $file . ': ' . md5_file($file);
?>
Siehe auch
- md5() - Errechnet den MD5-Hash eines Strings
- sha1_file() - Berechnet den SHA1-Hash einer Datei
- crc32() - Berechnet den polynomischen CRC32-Wert eines Strings
md5_file
potsed [at] gmail [dot] com
27-May-2007 01:12
27-May-2007 01:12
bubba at revbubba dot com
29-Mar-2005 02:56
29-Mar-2005 02: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 09:57
16-Nov-2004 09: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));