note 94494 added to function.md5-file

From: Date: Sun, 08 Nov 2009 15:24:03 +0000
Subject: note 94494 added to function.md5-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-162635@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 69.69.21.91 ---- Manual Page -- http://www.php.net/manual/en/function.md5-file.php Edit -- https://master.php.net/note/edit/94494 Del: integrated -- https://master.php.net/note/delete/94494/integrated Del: useless -- https://master.php.net/note/delete/94494/useless Del: bad code -- https://master.php.net/note/delete/94494/bad+code Del: spam -- https://master.php.net/note/delete/94494/spam Del: non-english -- https://master.php.net/note/delete/94494/non-english Del: in docs -- https://master.php.net/note/delete/94494/in+docs Del: other reasons-- https://master.php.net/note/delete/94494 Reject -- https://master.php.net/note/reject/94494 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#162635) next »