Current Location: Home> Latest Articles> How to use md5_file() with file_exists() function to determine whether the file exists and is available?

How to use md5_file() with file_exists() function to determine whether the file exists and is available?

M66 2025-06-06

In PHP, we often need to determine whether a file exists and further verify whether the file is valid or not tampered with. file_exists() is a very common function that can quickly determine whether a file exists in a specified path. However, just the existence of the file does not mean that its content is what we expect. At this time, we can combine md5_file() to perform content verification.

Basic usage

Let’s first look at the basic usage method of file_exists() :

 $filePath = '/path/to/your/file.txt';

if (file_exists($filePath)) {
    echo "The file exists。";
} else {
    echo "The file does not exist。";
}

This function simply determines whether there is a file on the path. If you need to further determine whether this file is the one we expect, you can verify it by calculating its MD5 value by md5_file() :

 $expectedHash = '5d41402abc4b2a76b9719d911017c592'; // Expected file hash value
$actualHash = md5_file($filePath);

if ($actualHash === $expectedHash) {
    echo "The file is valid and has not been tampered with。";
} else {
    echo "Files may be modified or corrupted。";
}

Actual case: Verify the integrity after downloading the file

Suppose we downloaded a compressed package from https://m66.net/files/update.zip , we want to make sure the file is downloaded successfully and has not been modified halfway. At this time, you can use file_exists() combined with md5_file() :

 $url = 'https://m66.net/files/update.zip';
$localPath = __DIR__ . '/downloads/update.zip';
$expectedMd5 = 'c4ca4238a0b923820dcc509a6f75849b'; // File hash value from the server

if (file_exists($localPath)) {
    if (md5_file($localPath) === $expectedMd5) {
        echo "The file exists且完整。";
    } else {
        echo "The file exists但不完整,Consider re-downloading。";
    }
} else {
    echo "The file does not exist,Please download it first:$url";
}

Things to note

  1. md5_file() will read the entire file, so for large files it may be slower and consume more memory.

  2. In environments with high security requirements, it is recommended to use a more secure hashing algorithm such as SHA-256 (which can be implemented using hash_file() function).

  3. md5_file() will return false when the file does not exist. You can use the following method to avoid misjudgment:

 $hash = file_exists($localPath) ? md5_file($localPath) : false;

Summarize

Combining file_exists() and md5_file() can effectively determine whether the file exists and verify its integrity, which is particularly suitable for handling scenarios such as file download, cache verification, and file tamper-proof. By checking the hash value, we can confirm with more confidence that the local file is consistent with the server or the original file. This method is simple but very practical, and is one of the basic skills that every PHP developer should master.