Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FilesSize.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/File/Size.php';
26 
36 {
40  const TOO_BIG = 'fileFilesSizeTooBig';
41  const TOO_SMALL = 'fileFilesSizeTooSmall';
42  const NOT_READABLE = 'fileFilesSizeNotReadable';
43 
47  protected $_messageTemplates = array(
48  self::TOO_BIG => "All files in sum should have a maximum size of '%max%' but '%size%' were detected",
49  self::TOO_SMALL => "All files in sum should have a minimum size of '%min%' but '%size%' were detected",
50  self::NOT_READABLE => "One or more files can not be read",
51  );
52 
58  protected $_files;
59 
69  public function __construct($options)
70  {
71  $this->_files = array();
72  $this->_setSize(0);
73 
74  if ($options instanceof Zend_Config) {
75  $options = $options->toArray();
76  } elseif (is_scalar($options)) {
77  $options = array('max' => $options);
78  } elseif (!is_array($options)) {
79  #require_once 'Zend/Validate/Exception.php';
80  throw new Zend_Validate_Exception('Invalid options to validator provided');
81  }
82 
83  if (1 < func_num_args()) {
84  $argv = func_get_args();
85  array_shift($argv);
86  $options['max'] = array_shift($argv);
87  if (!empty($argv)) {
88  $options['bytestring'] = array_shift($argv);
89  }
90  }
91 
92  parent::__construct($options);
93  }
94 
105  public function isValid($value, $file = null)
106  {
107  #require_once 'Zend/Loader.php';
108  if (is_string($value)) {
109  $value = array($value);
110  }
111 
112  $min = $this->getMin(true);
113  $max = $this->getMax(true);
114  $size = $this->_getSize();
115  foreach ($value as $files) {
116  // Is file readable ?
118  $this->_throw($file, self::NOT_READABLE);
119  continue;
120  }
121 
122  if (!isset($this->_files[$files])) {
123  $this->_files[$files] = $files;
124  } else {
125  // file already counted... do not count twice
126  continue;
127  }
128 
129  // limited to 2GB files
130  $size += @filesize($files);
131  $this->_size = $size;
132  if (($max !== null) && ($max < $size)) {
133  if ($this->useByteString()) {
134  $this->_max = $this->_toByteString($max);
135  $this->_size = $this->_toByteString($size);
136  $this->_throw($file, self::TOO_BIG);
137  $this->_max = $max;
138  $this->_size = $size;
139  } else {
140  $this->_throw($file, self::TOO_BIG);
141  }
142  }
143  }
144 
145  // Check that aggregate files are >= minimum size
146  if (($min !== null) && ($size < $min)) {
147  if ($this->useByteString()) {
148  $this->_min = $this->_toByteString($min);
149  $this->_size = $this->_toByteString($size);
150  $this->_throw($file, self::TOO_SMALL);
151  $this->_min = $min;
152  $this->_size = $size;
153  } else {
154  $this->_throw($file, self::TOO_SMALL);
155  }
156  }
157 
158  return empty($this->_messages);
159  }
160 }
getMax($raw=false)
Definition: Size.php:207
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
static isReadable($filename)
Definition: Loader.php:162
$value
Definition: gender.phtml:16
getMin($raw=false)
Definition: Size.php:165
isValid($value, $file=null)
Definition: FilesSize.php:105
_throw($file, $errorType)
Definition: Size.php:392
foreach($appDirs as $dir) $files