Hudzilla.org - the homepage of Paul Hudson
Contents > Files Wish List | Report Bug | About Me ]

8.15     File checksums: sha1_file() and md5_file()

This is NOT the latest copy of this book; click here for the latest version.

string sha1_file ( string filename [, bool raw_output])

string md5_file ( string filename [, bool raw_output])

Back in earlier days of the Internet, most files took a long time to download, and users frequently found they ended up with corrupted downloads. Even today, with ADSL and cable modems being common place, many downloads take a little time to complete, and errors still do occur.

To combat the chance of people downloading bad files and not realising, and also to make sure people know they have an official copy of a file/program, it became popular practice to create a checksum hash value using algorithms such as SHA1. By attaching a hash value this way allows downloaders to check their copy of a file to make sure it matches the web site's hash value - if it does not, the file is not the same.

Because this process of checksumming files is so common, there is a function specifically for it, sha1_file(), which works in roughly the same way as the sha1() function we looked at previously - you feed it the filename of a file you want to checksum, and it returns the SHA1 hash value.

Given the variable $filename is set to a valid filename, these two lines of code are functionally identical:

<?php
    $sha1
= sha1_file($filename);
    
$sha1 = sha1(file_get_contents($filename));
?>

The only real difference between the two is that the first line is about 20% faster than the second.

For MD5 hashing, you can use the function md5_file(). It works in exactly the same way as sha1_file(), except that it returns the MD5 hash as opposed to the SHA1 hash.





<< 8.14 Remote files   8.16 Parsing a configuration file: parse_ini_file() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow