PHP md5_file() Function
In this chapter you will learn:
- Definition for PHP md5_file() Function
- Syntax for PHP md5_file() Function
- Parameter for PHP md5_file() Function
- Return for PHP md5_file() Function
- Example - Calculate the MD5 hash of the text file "test.txt"
- Example - Store the MD5 hash of "test.txt" in a file and Test if "test.txt" has been changed
Definition
The md5_file() function calculates the MD5 hash of a file.
To calculate the MD5 hash of a string, use the md5() function.
Syntax
PHP md5_file() Function has the following syntax.
md5_file(file,raw)
Parameter
Parameter | Is Required | Description |
---|---|---|
file | Required. | The file to be calculated |
raw | Optional. | A boolean value that specifies hex or binary output format: |
Possible values for raw:
- TRUE - Raw 16 character binary format
- FALSE - Default. 32 character hex number
Return
Returns the calculated MD5 hash on success, or FALSE on failure.
Example 1
Calculate the MD5 hash of the text file "test.txt":
<?php/*from jav a2 s .c o m*/
$filename = "test.txt";
$md5file = md5_file($filename);
echo $md5file;
?>
The code above generates the following result.
Example 2
Store the MD5 hash of "test.txt" in a file and Test if "test.txt" has been changed.
<?php// j a va 2s. com
$md5file = md5_file("test.txt");
file_put_contents("md5file.txt",$md5file);
$md5file = file_get_contents("md5file.txt");
if (md5_file("test.txt") == $md5file){
echo "The file is ok.";
}else{
echo "The file has been changed.";
}
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP mkdir() Function
- Syntax for PHP mkdir() Function
- Parameter for PHP mkdir() Function
- Return for PHP mkdir() Function
- Example - The mkdir() function creates a directory.
Home » PHP Tutorial » PHP File Functions