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-2010 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-2010 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
=> "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 $argv = func_get_args();
117 $options['max'] = array_shift($argv);
119 $options['bytestring'] = array_shift($argv);
123 if (isset($options['bytestring'])) {
124 $this->setUseByteString($options['bytestring']);
127 if (isset($options['min'])) {
128 $this->setMin($options['min']);
131 if (isset($options['max'])) {
132 $this->setMax($options['max']);
137 * Returns the minimum filesize
139 * @param boolean $byteString Use bytestring ?
142 public function setUseByteString($byteString = true)
144 $this->_useByteString
= (bool) $byteString;
149 * Will bytestring be used?
153 public function useByteString()
155 return $this->_useByteString
;
159 * Returns the minimum filesize
161 * @param bool $raw Whether or not to force return of the raw value (defaults off)
162 * @return integer|string
164 public function getMin($raw = false)
167 if (!$raw && $this->useByteString()) {
168 $min = $this->_toByteString($min);
175 * Sets the minimum filesize
177 * @param integer $min The minimum filesize
178 * @throws Zend_Validate_Exception When min is greater than max
179 * @return Zend_Validate_File_Size Provides a fluent interface
181 public function setMin($min)
183 if (!is_string($min) and !is_numeric($min)) {
184 require_once 'Zend/Validate/Exception.php';
185 throw new Zend_Validate_Exception ('Invalid options to validator provided');
188 $min = (integer) $this->_fromByteString($min);
189 $max = $this->getMax(true);
190 if (($max !== null) && ($min > $max)) {
191 require_once 'Zend/Validate/Exception.php';
192 throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum filesize, but $min >"
201 * Returns the maximum filesize
203 * @param bool $raw Whether or not to force return of the raw value (defaults off)
204 * @return integer|string
206 public function getMax($raw = false)
209 if (!$raw && $this->useByteString()) {
210 $max = $this->_toByteString($max);
217 * Sets the maximum filesize
219 * @param integer $max The maximum filesize
220 * @throws Zend_Validate_Exception When max is smaller than min
221 * @return Zend_Validate_StringLength Provides a fluent interface
223 public function setMax($max)
225 if (!is_string($max) && !is_numeric($max)) {
226 require_once 'Zend/Validate/Exception.php';
227 throw new Zend_Validate_Exception ('Invalid options to validator provided');
230 $max = (integer) $this->_fromByteString($max);
231 $min = $this->getMin(true);
232 if (($min !== null) && ($max < $min)) {
233 require_once 'Zend/Validate/Exception.php';
234 throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum filesize, but "
243 * Retrieve current detected file size
247 protected function _getSize()
256 * @return Zend_Validate_File_Size
258 protected function _setSize($size)
260 $this->_size
= $size;
265 * Defined by Zend_Validate_Interface
267 * Returns true if and only if the filesize of $value is at least min and
268 * not bigger than max (when max is not null).
270 * @param string $value Real file to check for size
271 * @param array $file File data from Zend_File_Transfer
274 public function isValid($value, $file = null)
276 // Is file readable ?
277 require_once 'Zend/Loader.php';
278 if (!Zend_Loader
::isReadable($value)) {
279 return $this->_throw($file, self
::NOT_FOUND
);
282 // limited to 4GB files
283 $size = sprintf("%u", @filesize
($value));
284 $this->_size
= $size;
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);