PHP Doku:: Berechnet den MD5-Code einer Datei - function.md5-file.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzTextverarbeitungZeichenkettenString-Funktionenmd5_file

Ein Service von Reinhard Neidl - Webprogrammierung.

String-Funktionen

<<ltrim

md5>>

md5_file

(PHP 4 >= 4.2.0, PHP 5)

md5_fileBerechnet 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


8 BenutzerBeiträge:
- Beiträge aktualisieren...
Chris
8.11.2009 16:24
If you just need to find out if two files are identical, comparing file hashes can be inefficient, especially on large files.  There's no reason to read two whole files and do all the math if the second byte of each file is different.  If you don't need to store the hash value for later use, there may not be a need to calculate the hash value just to compare files.  This can be much faster:

<?php
define
('READ_LEN', 4096);

if(
files_identical('file1.txt', 'file2.txt'))
    echo
'files identical';
else
    echo
'files not identical';

//   pass two file names
//   returns TRUE if files are the same, FALSE otherwise
function files_identical($fn1, $fn2) {
    if(
filetype($fn1) !== filetype($fn2))
        return
FALSE;

    if(
filesize($fn1) !== filesize($fn2))
        return
FALSE;

    if(!
$fp1 = fopen($fn1, 'rb'))
        return
FALSE;

    if(!
$fp2 = fopen($fn2, 'rb')) {
       
fclose($fp1);
        return
FALSE;
    }

   
$same = TRUE;
    while (!
feof($fp1) and !feof($fp2))
        if(
fread($fp1, READ_LEN) !== fread($fp2, READ_LEN)) {
           
$same = FALSE;
            break;
        }

    if(
feof($fp1) !== feof($fp2))
       
$same = FALSE;

   
fclose($fp1);
   
fclose($fp2);

    return
$same;
}
?>
Sebastian Zavadschi (php.box.md)
22.09.2008 10:55
I need to parse a big file after it was changed (a shared XLS file). I've fond a good idea to check once a minute the checksum of the file, in order to track if it was changed or not. Here is my code, hope will help some one. Any improvements are welcome!

<?php
$file_xls
= "test.xls";
$file_md5 = "test.xls.md5"; // Must exists and must be writable for PHP
$md5_new_file = trim(md5_file($file_xls));
$md5_old_file = trim(file_get_contents($file_md5));

if(
$md5_new_file <> $md5_old_file)
    {
    echo
"file is out of date, updating now...";
   
rename($file_md5, $file_md5.".bak");
   
$fp = fopen($file_md5, 'w');
   
fwrite($fp, $md5_new_file);
   
fclose($fp);
/*
Here we do some job...
In my case - dealing with "Spreadsheet Excel Reader"
*/
   
unlink($file_md5.".bak");
    }
// "Not for crontab" - Remove the following section if you are intending to run it in crontab
else
    {
    echo
"file is up to date, nothing to do...";
    }
// End "Not for crontab";
?>
smartin
12.03.2008 13:58
In response to using exec instead for performance (Nov 13 2007 post), It looks like the performance depends on the size of the file.  See the results below using the same script from the original post.  The first hash is with md5_file and the second is with openssl md5.

With a 1MB file:
Hash = df1555ec0c2d7fcad3a03770f9aa238a; time = 0.005006
Hash = df1555ec0c2d7fcad3a03770f9aa238a; time = 0.01498

With a 2MB file:

Hash = 4387904830a4245a8ab767e5937d722c; time = 0.010393
Hash = 4387904830a4245a8ab767e5937d722c; time = 0.016691

With a 10MB file:

Hash = b89f948e98f3a113dc13fdbd3bdb17ef; time = 0.241907
Hash = b89f948e98f3a113dc13fdbd3bdb17ef; time = 0.037597

Performance seems to change proportionally with the file size.  Judging from the previous post's default file name (.mov) he/she was probably dealing with a large file.  These are just quick tests and far from a perfect benchmark, but you might want to test your own files before assuming that the openssl solution is faster (ie, if working with small text files vs. movies, etc)
toby at globaloptima dot co dot uk
18.11.2007 21:15
I'm wondering about the MD5_DIR function posted by potsed, what happens if the file listing is returned in a different order?

From what I can tell you get different MD5's based on the order, a minor addition sorts this:

    ...
    asort($filemd5s); //sort the md5s before concat
    return md5(implode('', $filemd5s));
}
glau dot stuff at N0_SPAM dot ridiculousprods dot com
14.11.2007 2:54
It's much faster to call an 'exec' command to openssl md5 than to use md5_file.

<?php

$file_path
= '/path/to/large/video_file.mov';

$begin = microtime(true);

$hash = md5_file($file_path);

$end = microtime(true) - $begin;

echo
"Hash = $hash; time = $end<br>";
# Hash = eac425a6f5b90f69e74710b015228640; time = 2.5333859920502

$begin = microtime(true);

$result = split('=',exec("openssl md5 $file_path"));

$end = microtime(true) - $begin;

echo
"Hash = ".$result[1]."; time = $end";
#Hash = eac425a6f5b90f69e74710b015228640; time = 0.79528999328613

?>

I consistently see about a 3x improvement in speed.
potsed [at] gmail [dot] com
27.05.2007 13:12
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));
}
bubba at revbubba dot com
29.03.2005 14: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.11.2004 9: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));



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)