note 104748 added to function.sha1-file

From: Date: Tue, 05 Jul 2011 14:13:33 +0000
Subject: note 104748 added to function.sha1-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-181252@lists.php.net to get a copy of this message
Just for the record - As some have pointed out, you have two ways to generate the hash of a file: Method 1 [this function]: sha1_file($file) Method 2: sha1(file_get_contents($file)) It's important to realize that these two methods are NOT the same thing. If they were, I seriously doubt this function would exist. The key difference, as far as I can tell, is how the file's contents are loaded. The second method loads the entirety of $file into memory before passing it to sha1($str). Method two, however, loads the contents of $file as they are needed to create the hash. If you can guarantee that you'll only ever have to hash relatively small files, this difference means very little. If you have larger ones, though, loading the entirety of file into memory is a bad idea: best case, you slow down your server as it tries to handle the request; worse case, you run out of memory and don't get your hash at all. Just try to keep this in mind if you decide to load the file's contents yourself, in lieu of using this function. On my system, I was able to use this function to generate the hash of a 2.6GB file in 22 seconds, whereas I could not with the second method, due to an out-of-memory error (which took 185 seconds). ---- Server IP: 64.71.164.2 Probable Submitter: 71.53.6.250 ---- Manual Page -- http://www.php.net/manual/en/function.sha1-file.php Edit -- https://master.php.net/note/edit/104748 Del: integrated -- https://master.php.net/note/delete/104748/integrated Del: useless -- https://master.php.net/note/delete/104748/useless Del: bad code -- https://master.php.net/note/delete/104748/bad+code Del: spam -- https://master.php.net/note/delete/104748/spam Del: non-english -- https://master.php.net/note/delete/104748/non-english Del: in docs -- https://master.php.net/note/delete/104748/in+docs Del: other reasons-- https://master.php.net/note/delete/104748 Reject -- https://master.php.net/note/reject/104748 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#181252) next »