Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
115 lars 1
<?php
2
 
3
/**
4
 * Project:     SmartyValidate: Form Validator for the Smarty Template Engine
5
 * File:        validate_criteria.isFileSize.php
6
 * Author:      Monte Ohrt <monte at newdigitalgroup dot com>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
 *
22
 * @link http://www.phpinsider.com/php/code/SmartyValidate/
23
 * @copyright 2001-2005 New Digital Group, Inc.
24
 * @author Monte Ohrt <monte at newdigitalgroup dot com>
25
 * @package SmartyValidate
26
 */
27
 
28
/**
29
 * test if a value is a valid file size.
30
 *
31
 * @param string $value the value being tested
32
 * @param boolean $empty if field can be empty
33
 * @param array params validate parameter values
34
 * @param array formvars form var values
35
 */
36
function smarty_validate_criteria_isFileSize($value, $empty, &$params, &$formvars) {
37
 
38
    $_field = $params['field'];
39
    $_max = isset($params['field2']) ? $params['field2'] : trim($params['max']);
40
 
41
 
42
    if(!isset($_FILES[$_field]))
43
        // nothing in the form
44
        return false;
45
 
46
    if($_FILES[$_field]['error'] == 4)
47
        // no file uploaded
48
        return $empty;
49
 
50
    if(!isset($_max)) {
51
        trigger_error("SmartyValidate: [isFileSize] 'max' attribute is missing.");
52
        return false;
53
    }
54
 
55
    if(!preg_match('!^(\d+)([bkmg](b)?)?$!i', $_max, $_match)) {
56
        trigger_error("SmartyValidate: [isFileSize] 'max' attribute is invalid.");
57
        return false;
58
    }
59
 
60
    $_size = $_match[1];
61
    $_type = strtolower($_match[2]);
62
 
63
    switch($_type) {
64
        case 'k':
65
            $_maxsize = $_size * 1024;
66
            break;
67
        case 'm':
68
            $_maxsize = $_size * 1024 * 1024;
69
            break;
70
        case 'g':
71
            $_maxsize = $_size * 1024 * 1024 * 1024;
72
            break;
73
        case 'b':
74
        default:
75
            $_maxsize = $_size;
76
            break;
77
    }
78
 
79
    return $_FILES[$_field]['size'] <= $_maxsize;
80
}
81
 
82
?>