source: branches/version-2_5-dev/data/module/Compat/Compat/Function/md5_file.php @ 19792

Revision 19792, 2.9 KB checked in by Seasoft, 13 years ago (diff)

#744(PHP4 互換用途ソースを将来的に切り捨てやすい仕組みづくり)

Line 
1<?php
2// +----------------------------------------------------------------------+
3// | PHP Version 4                                                        |
4// +----------------------------------------------------------------------+
5// | Copyright (c) 1997-2004 The PHP Group                                |
6// +----------------------------------------------------------------------+
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        |
9// | available at through the world-wide-web at                           |
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   |
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.               |
14// +----------------------------------------------------------------------+
15// | Authors: Aidan Lister <aidan@php.net>                                |
16// +----------------------------------------------------------------------+
17//
18// $Id: md5_file.php,v 1.3 2005/11/22 08:29:19 aidan Exp $
19
20
21/**
22 * Replace md5_file()
23 *
24 * @category    PHP
25 * @package     PHP_Compat
26 * @link        http://php.net/md5_file
27 * @author      Aidan Lister <aidan@php.net>
28 * @version     $Revision: 1.3 $
29 * @since       PHP 4.2.0
30 * @require     PHP 4.0.0 (user_error)
31 */
32if (!function_exists('md5_file')) {
33    function md5_file($filename, $raw_output = false)
34    {
35        // Sanity check
36        if (!is_scalar($filename)) {
37            user_error('md5_file() expects parameter 1 to be string, ' .
38                gettype($filename) . ' given', E_USER_WARNING);
39            return;
40        }
41
42        if (!is_scalar($raw_output)) {
43            user_error('md5_file() expects parameter 2 to be bool, ' .
44                gettype($raw_output) . ' given', E_USER_WARNING);
45            return;
46        }
47
48        if (!file_exists($filename)) {
49            user_error('md5_file() Unable to open file', E_USER_WARNING);
50            return false;
51        }
52       
53        // Read the file
54        if (false === $fh = fopen($filename, 'rb')) {
55            user_error('md5_file() failed to open stream: No such file or directory',
56                E_USER_WARNING);
57            return false;
58        }
59
60        clearstatcache();
61        if ($fsize = @filesize($filename)) {
62            $data = fread($fh, $fsize);
63        } else {
64            $data = '';
65            while (!feof($fh)) {
66                $data .= fread($fh, 8192);
67            }
68        }
69
70        fclose($fh);
71
72        // Return
73        $data = md5($data);
74        if ($raw_output === true) {
75            $data = pack('H*', $data);
76        }
77
78        return $data;
79    }
80}
81
82?>
Note: See TracBrowser for help on using the repository browser.