7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Validate
17 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * @see Zend_Validate_Abstract
25 require_once 'Zend/Validate/Abstract.php';
28 * Validator for the maximum size of a file up to a max of 2GB
31 * @package Zend_Validate
32 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
35 class Zend_Validate_File_Size
extends Zend_Validate_Abstract
38 * @const string Error constants
40 const TOO_BIG
= 'fileSizeTooBig';
41 const TOO_SMALL
= 'fileSizeTooSmall';
42 const NOT_FOUND
= 'fileSizeNotFound';
46 * @var array Error message templates
48 protected $_messageTemplates = array(
49 self
::TOO_BIG
=> "Maximum allowed size for file '%value%' is '%max%' but '%size%' detected",
50 self
::TOO_SMALL
=> "Minimum expected size for file '%value%' is '%min%' but '%size%' detected",
51 self
::NOT_FOUND
=> "The file '%value%' could not be found"
55 * @var array Error message template variables
57 protected $_messageVariables = array(
72 * If null, there is no maximum filesize
90 protected $_useByteString = true;
93 * Sets validator options
95 * If $options is a integer, it will be used as maximum filesize
96 * As Array is accepts the following keys:
97 * 'min': Minimum filesize
98 * 'max': Maximum filesize
99 * 'bytestring': Use bytestring or real size for messages
101 * @param integer|array $options Options for the adapter
103 public function __construct($options)
105 if ($options instanceof Zend_Config
) {
106 $options = $options->toArray();
107 } elseif (is_string($options) ||
is_numeric($options)) {
108 $options = array('max' => $options);
109 } elseif (!is_array($options)) {
110 require_once 'Zend/Validate/Exception.php';
111 throw new Zend_Validate_Exception ('Invalid options to validator provided');
114 if (1 < func_num_args()) {
115 trigger_error('Multiple constructor options are deprecated in favor of a single options array', E_USER_NOTICE
);
116 $argv = func_get_args();
118 $options['max'] = array_shift($argv);
120 $options['bytestring'] = array_shift($argv);
124 if (isset($options['bytestring'])) {
125 $this->setUseByteString($options['bytestring']);
128 if (isset($options['min'])) {
129 $this->setMin($options['min']);
132 if (isset($options['max'])) {
133 $this->setMax($options['max']);
138 * Returns the minimum filesize
140 * @param boolean $byteString Use bytestring ?
143 public function setUseByteString($byteString = true)
145 $this->_useByteString
= (bool) $byteString;
150 * Will bytestring be used?
154 public function useByteString()
156 return $this->_useByteString
;
160 * Returns the minimum filesize
162 * @param bool $raw Whether or not to force return of the raw value (defaults off)
163 * @return integer|string
165 public function getMin($raw = false)
168 if (!$raw && $this->useByteString()) {
169 $min = $this->_toByteString($min);
176 * Sets the minimum filesize
178 * @param integer $min The minimum filesize
179 * @throws Zend_Validate_Exception When min is greater than max
180 * @return Zend_Validate_File_Size Provides a fluent interface
182 public function setMin($min)
184 if (!is_string($min) and !is_numeric($min)) {
185 require_once 'Zend/Validate/Exception.php';
186 throw new Zend_Validate_Exception ('Invalid options to validator provided');
189 $min = (integer) $this->_fromByteString($min);
190 $max = $this->getMax(true);
191 if (($max !== null) && ($min > $max)) {
192 require_once 'Zend/Validate/Exception.php';
193 throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum filesize, but $min >"
202 * Returns the maximum filesize
204 * @param bool $raw Whether or not to force return of the raw value (defaults off)
205 * @return integer|string
207 public function getMax($raw = false)
210 if (!$raw && $this->useByteString()) {
211 $max = $this->_toByteString($max);
218 * Sets the maximum filesize
220 * @param integer $max The maximum filesize
221 * @throws Zend_Validate_Exception When max is smaller than min
222 * @return Zend_Validate_StringLength Provides a fluent interface
224 public function setMax($max)
226 if (!is_string($max) && !is_numeric($max)) {
227 require_once 'Zend/Validate/Exception.php';
228 throw new Zend_Validate_Exception ('Invalid options to validator provided');
231 $max = (integer) $this->_fromByteString($max);
232 $min = $this->getMin(true);
233 if (($min !== null) && ($max < $min)) {
234 require_once 'Zend/Validate/Exception.php';
235 throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum filesize, but "
244 * Retrieve current detected file size
248 protected function _getSize()
257 * @return Zend_Validate_File_Size
259 protected function _setSize($size)
261 $this->_size
= $size;
266 * Defined by Zend_Validate_Interface
268 * Returns true if and only if the filesize of $value is at least min and
269 * not bigger than max (when max is not null).
271 * @param string $value Real file to check for size
272 * @param array $file File data from Zend_File_Transfer
275 public function isValid($value, $file = null)
277 // Is file readable ?
278 require_once 'Zend/Loader.php';
279 if (!Zend_Loader
::isReadable($value)) {
280 return $this->_throw($file, self
::NOT_FOUND
);
283 // limited to 4GB files
284 $size = sprintf("%u", @filesize
($value));
286 // Check to see if it's smaller than min size
287 $min = $this->getMin(true);
288 $max = $this->getMax(true);
289 if (($min !== null) && ($size < $min)) {
290 if ($this->useByteString()) {
291 $this->_min
= $this->_toByteString($min);
292 $this->_size
= $this->_toByteString($size);
293 $this->_throw($file, self
::TOO_SMALL
);
295 $this->_size
= $size;
297 $this->_throw($file, self
::TOO_SMALL
);
301 // Check to see if it's larger than max size
302 if (($max !== null) && ($max < $size)) {
303 if ($this->useByteString()) {
304 $this->_max
= $this->_toByteString($max);
305 $this->_size
= $this->_toByteString($size);
306 $this->_throw($file, self
::TOO_BIG
);
308 $this->_size
= $size;
310 $this->_throw($file, self
::TOO_BIG
);
314 if (count($this->_messages
) > 0) {
322 * Returns the formatted size
324 * @param integer $size
327 protected function _toByteString($size)
329 $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
330 for ($i=0; $size >= 1024 && $i < 9; $i++
) {
334 return round($size, 2) . $sizes[$i];
338 * Returns the unformatted size
340 * @param string $size
343 protected function _fromByteString($size)
345 if (is_numeric($size)) {
346 return (integer) $size;
349 $type = trim(substr($size, -2, 1));
351 $value = substr($size, 0, -1);
352 if (!is_numeric($value)) {
353 $value = substr($value, 0, -1);
356 switch (strtoupper($type)) {
358 $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
361 $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
364 $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024);
367 $value *= (1024 * 1024 * 1024 * 1024 * 1024);
370 $value *= (1024 * 1024 * 1024 * 1024);
373 $value *= (1024 * 1024 * 1024);
376 $value *= (1024 * 1024);
389 * Throws an error of the given type
391 * @param string $file
392 * @param string $errorType
395 protected function _throw($file, $errorType)
397 if ($file !== null) {
398 $this->_value
= $file['name'];
401 $this->_error($errorType);