Rev 130 Rev 139
1 <?php 1 <?php
2 // +----------------------------------------------------------------------+ 2 // +----------------------------------------------------------------------+
3 // | PHP Version 4 | 3 // | PHP Version 4 |
4 // +----------------------------------------------------------------------+ 4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 1997-2004 The PHP Group | 5 // | Copyright (c) 1997-2004 The PHP Group |
6 // +----------------------------------------------------------------------+ 6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 3.0 of the PHP license, | 7 // | This source file is subject to version 3.0 of the PHP license, |
8 // | that is bundled with this package in the file LICENSE, and is | 8 // | that is bundled with this package in the file LICENSE, and is |
9 // | available at through the world-wide-web at | 9 // | available at through the world-wide-web at |
10 // | http://www.php.net/license/3_0.txt. | 10 // | http://www.php.net/license/3_0.txt. |
11 // | If you did not receive a copy of the PHP license and are unable to | 11 // | If you did not receive a copy of the PHP license and are unable to |
12 // | obtain it through the world-wide-web, please send a note to | 12 // | obtain it through the world-wide-web, please send a note to |
13 // | license@php.net so we can mail you a copy immediately. | 13 // | license@php.net so we can mail you a copy immediately. |
14 // +----------------------------------------------------------------------+ 14 // +----------------------------------------------------------------------+
15 // | Authors: Aidan Lister <aidan@php.net> | 15 // | Authors: Aidan Lister <aidan@php.net> |
16 // +----------------------------------------------------------------------+ 16 // +----------------------------------------------------------------------+
17 // 17 //
18 // $Id: md5_file.php,v 1.3 2005/11/22 08:29:19 aidan Exp $ 18 // $Id: md5_file.php,v 1.3 2005/11/22 08:29:19 aidan Exp $
19   19  
20   20  
21 /** 21 /**
22 * Replace md5_file() 22 * Replace md5_file()
23 * 23 *
24 * @category PHP 24 * @category PHP
25 * @package PHP_Compat 25 * @package PHP_Compat
26 * @link http://php.net/md5_file 26 * @link http://php.net/md5_file
27 * @author Aidan Lister <aidan@php.net> 27 * @author Aidan Lister <aidan@php.net>
28 * @version $Revision: 1.3 $ 28 * @version $Revision: 1.3 $
29 * @since PHP 4.2.0 29 * @since PHP 4.2.0
30 * @require PHP 4.0.0 (user_error) 30 * @require PHP 4.0.0 (user_error)
31 */ 31 */
32 if (!function_exists('md5_file')) { 32 if (!function_exists('md5_file')) {
33 function md5_file($filename, $raw_output = false) 33 function md5_file($filename, $raw_output = false)
34 { 34 {
35 // Sanity check 35 // Sanity check
36 if (!is_scalar($filename)) { 36 if (!is_scalar($filename)) {
37 user_error('md5_file() expects parameter 1 to be string, ' . 37 user_error('md5_file() expects parameter 1 to be string, ' .
38 gettype($filename) . ' given', E_USER_WARNING); 38 gettype($filename) . ' given', E_USER_WARNING);
39 return; 39 return;
40 } 40 }
41   41  
42 if (!is_scalar($raw_output)) { 42 if (!is_scalar($raw_output)) {
43 user_error('md5_file() expects parameter 2 to be bool, ' . 43 user_error('md5_file() expects parameter 2 to be bool, ' .
44 gettype($raw_output) . ' given', E_USER_WARNING); 44 gettype($raw_output) . ' given', E_USER_WARNING);
45 return; 45 return;
46 } 46 }
47   47  
48 if (!file_exists($filename)) { 48 if (!file_exists($filename)) {
49 user_error('md5_file() Unable to open file', E_USER_WARNING); 49 user_error('md5_file() Unable to open file', E_USER_WARNING);
50 return false; 50 return false;
51 } 51 }
52 52
53 // Read the file 53 // Read the file
54 if (false === $fh = fopen($filename, 'rb')) { 54 if (false === $fh = fopen($filename, 'rb')) {
55 user_error('md5_file() failed to open stream: No such file or directory', 55 user_error('md5_file() failed to open stream: No such file or directory',
56 E_USER_WARNING); 56 E_USER_WARNING);
57 return false; 57 return false;
58 } 58 }
59   59  
60 clearstatcache(); 60 clearstatcache();
61 if ($fsize = @filesize($filename)) { 61 if ($fsize = @filesize($filename)) {
62 $data = fread($fh, $fsize); 62 $data = fread($fh, $fsize);
63 } else { 63 } else {
64 $data = ''; 64 $data = '';
65 while (!feof($fh)) { 65 while (!feof($fh)) {
66 $data .= fread($fh, 8192); 66 $data .= fread($fh, 8192);
67 } 67 }
68 } 68 }
69   69  
70 fclose($fh); 70 fclose($fh);
71   71  
72 // Return 72 // Return
73 $data = md5($data); 73 $data = md5($data);
74 if ($raw_output === true) { 74 if ($raw_output === true) {
75 $data = pack('H*', $data); 75 $data = pack('H*', $data);
76 } 76 }
77   77  
78 return $data; 78 return $data;
79 } 79 }
80 } 80 }
81   81  
82 ?> 82 ?>
83 83