Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Size.php
Go to the documentation of this file.
1 <?php
10 namespace Magento\Framework\File;
11 
16 class Size
17 {
23  private $dataSize;
24 
31  protected static $_maxFileSize = -1;
32 
38  public function getPostMaxSize()
39  {
40  return $this->_iniGet('post_max_size');
41  }
42 
48  public function getUploadMaxSize()
49  {
50  return $this->_iniGet('upload_max_filesize');
51  }
52 
60  public function getMaxFileSizeInMb($precision = 0, $mode = \PHP_ROUND_HALF_DOWN)
61  {
62  return $this->getFileSizeInMb($this->getMaxFileSize(), $precision, $mode);
63  }
64 
73  public function getFileSizeInMb($fileSize, $precision = 0, $mode = \PHP_ROUND_HALF_DOWN)
74  {
75  return round($fileSize / (1024 * 1024), $precision, $mode);
76  }
77 
83  public function getMaxFileSize()
84  {
85  if (self::$_maxFileSize < 0) {
86  $postMaxSize = $this->getDataSize()->convertSizeToBytes($this->getPostMaxSize());
87  $uploadMaxSize = $this->getDataSize()->convertSizeToBytes($this->getUploadMaxSize());
88  $min = max($postMaxSize, $uploadMaxSize);
89 
90  if ($postMaxSize > 0) {
91  $min = min($min, $postMaxSize);
92  }
93 
94  if ($uploadMaxSize > 0) {
95  $min = min($min, $uploadMaxSize);
96  }
97 
98  self::$_maxFileSize = $min;
99  }
100 
101  return self::$_maxFileSize;
102  }
103 
112  public function convertSizeToInteger($size)
113  {
114  return $this->getDataSize()->convertSizeToBytes($size);
115  }
116 
124  protected function _iniGet($param)
125  {
126  return trim(ini_get($param));
127  }
128 
136  private function getDataSize()
137  {
138  if ($this->dataSize === null) {
139  $this->dataSize =
140  \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Convert\DataSize::class);
141  }
142 
143  return $this->dataSize;
144  }
145 }
getMaxFileSizeInMb($precision=0, $mode=\PHP_ROUND_HALF_DOWN)
Definition: Size.php:60
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
getFileSizeInMb($fileSize, $precision=0, $mode=\PHP_ROUND_HALF_DOWN)
Definition: Size.php:73