note 80487 added to function.crc32

From: Date: Fri, 18 Jan 2008 14:54:25 +0000
Subject: note 80487 added to function.crc32
Groups: php.notes 
Request: Send a blank email to php-notes+get-135196@lists.php.net to get a copy of this message
From my tests noted below, md5_file() function is the best function for performing file verification. This will remain the case until a native crc32_file() function is added to PHP. file_crc() function that Bulk at bulksplace dot com posted is the most efficient solution on Windows for small and medium size files. It is most likely because file_get_contents() uses memory mapping techniques. Unfortunately on Linux (Fedora), the results were slightly better for md5_file(). sha1_file() on large files is slower than md5_file(). The time it takes for the __crc32_file() function found below is linear to the size of the file. I would avoid using __crc32_file(). The file_crc() function will fail when using the file_get_contents() if the file is larger than the PHP.ini memory_limit setting. Windows does not seem to use the memory_limit for file_get_contents(), but I did run into an error 'FATAL: emalloc(): Unable to allocate x bytes' when testing iso files. I ran the following tests on both WindowsXP and Fedora 4 machines. <?php // File verification tests by Angelo Mandato (angelo [at] mandato {period} com) // __crc32_file() is very slow, you can uncomment to test for yourself. //require_once('crc32_file.php'); // Copy and paste the contents of the crc32_file() code found on // the php.net crc32 PHP manual page in a new file and save // as crc32_file.php in the same directory as this script. // Get microseconds function GetMicrotime() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } // file_crc() - function to test function file_crc($file) { $file_string = file_get_contents($file); $crc = crc32($file_string); return sprintf("%u", $crc); } $Methods = array('sha1_file()', 'md5_file()', 'file_crc()'); if( function_exists('__crc32_file') ) $Methods[] = '__crc32_file()'; $directory = '/path/to/directory/'; // Don't forget trailing backslash. $files = scandir($directory); for( $method_index = 0; $method_index < count($Methods); $method_index++ ) { $start_time = GetMicrotime(); while( list($index,$file) = each($files) ) { if( $file != '.' && $file != '..' && is_file($directory.$file) ) { switch( $method_index ) { case 0: { // sha1_file() $value = sha1_file($directory.$file); }; break; case 1: { // md5_file() $value = md5_file($directory.$file); }; break; case 2: { // file_crc() $value = file_crc($directory.$file); }; break; case 3: { // __crc32_file() $value = __crc32_file($directory.$file); }; break; } } else // It is not part of our test results, lets remove it from the array { unset($files[$index]); } } $end_time = GetMicrotime(); echo sprintf("%s took %.03f seconds to calculate %d files.\n", $Methods[$method_index], $end_time-$start_time, count($files) ); reset($files); // Reset pointer in array } echo "file verification tests completed.\n"; ?> It would be nice if a crc32_file() function was added to PHP natively. I am not sure why anyone removed this post from this thread, but this is very useful information. and I believe it should remain. If you're the moderator and deleted this before, please contact me and explain why it was removed. This is not a question, a report of a bug nor a feature request. This is factual information that is useful to all. ---- Server IP: 208.69.120.35 Probable Submitter: 75.118.24.31 ---- Manual Page -- http://www.php.net/manual/en/function.crc32.php Edit -- https://master.php.net/note/edit/80487 Del: integrated -- https://master.php.net/note/delete/80487/integrated Del: useless -- https://master.php.net/note/delete/80487/useless Del: bad code -- https://master.php.net/note/delete/80487/bad+code Del: spam -- https://master.php.net/note/delete/80487/spam Del: non-english -- https://master.php.net/note/delete/80487/non-english Del: in docs -- https://master.php.net/note/delete/80487/in+docs Del: other reasons-- https://master.php.net/note/delete/80487 Reject -- https://master.php.net/note/reject/80487 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135196) next »