PHP.mk документација

md5_file

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.md5-file.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека function.md5-file.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
md5_file

Референца за `function.md5-file.php` со подобрена типографија и навигација.

function.md5-file.php

md5_file

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

md5_fileГо пресметува md5 хешот на дадена датотека

= NULL

md5_file(string $filename, bool $binary = false): string|false

Го пресметува MD5 хешот на датотеката наведена со filename параметарот користејќи го Пресметува MD5 хеш од, и го враќа тој хеш. Хешот е 32-карактерен хексадецимален број.

Параметри

filename

Името на датотеката

binary

Кога true, го враќа дигестот во суров бинарен формат со должина од 16.

Вратени вредности

Враќа стринг при успех, false otherwise.

Примери

Пример #1 Пример за употреба на md5_file()

<?php
$file
= '/examples/book.xml';

echo
'MD5 file hash of ' . $file . ': ' . md5_file($file);
?>

Види Исто така

  • hash_file() - Генерира вредност на хеш користејќи ја содржината на дадена датотека
  • hash_init() - Враќа список на регистрирани алгоритми за хеширање погодни за hash_hmac
  • md5() - Пресметај го md5 хешот на стринг

Белешки од корисници 3 белешки

Крис
пред 16 години
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;
}
?>
lukasamd на gmail точка com
пред 14 години
It's faster to use md5sum than openssl md5:

<?php
$begin = microtime(true);

$file_path = '../backup_file1.tar.gz';
$result = explode("  ", exec("md5sum $file_path"));
echo "Hash = ".$result[0]."<br />";

# Here 7 other big files (20-300 MB)

$end = microtime(true) - $begin;
echo "Time = $end";
# Time = 4.4475841522217 

#Method with openssl
# Time = 12.1463856900543
?>

About 3x faster
smartin
пред 18 години
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)
Навигација

Прелистувај сродни теми и функции.

На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.