1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Magento file size lib
*/
namespace Magento\Framework\File;
/**
* @api
* @since 100.0.2
*/
class Size
{
/**
* Data size converter
*
* @var \Magento\Framework\Convert\DataSize
*/
private $dataSize;
/**
* Maximum file size for MAX_FILE_SIZE attribute of a form
*
* @link http://www.php.net/manual/en/features.file-upload.post-method.php
* @var integer
*/
protected static $_maxFileSize = -1;
/**
* Get post max size
*
* @return string
*/
public function getPostMaxSize()
{
return $this->_iniGet('post_max_size');
}
/**
* Get upload max size
*
* @return string
*/
public function getUploadMaxSize()
{
return $this->_iniGet('upload_max_filesize');
}
/**
* Get max file size in megabytes
*
* @param int $precision
* @param int $mode
* @return float
*/
public function getMaxFileSizeInMb($precision = 0, $mode = \PHP_ROUND_HALF_DOWN)
{
return $this->getFileSizeInMb($this->getMaxFileSize(), $precision, $mode);
}
/**
* Get file size in megabytes
*
* @param int $fileSize
* @param int $precision
* @param int $mode
* @return float
*/
public function getFileSizeInMb($fileSize, $precision = 0, $mode = \PHP_ROUND_HALF_DOWN)
{
return round($fileSize / (1024 * 1024), $precision, $mode);
}
/**
* Get the maximum file size of the a form in bytes
*
* @return integer
*/
public function getMaxFileSize()
{
if (self::$_maxFileSize < 0) {
$postMaxSize = $this->getDataSize()->convertSizeToBytes($this->getPostMaxSize());
$uploadMaxSize = $this->getDataSize()->convertSizeToBytes($this->getUploadMaxSize());
$min = max($postMaxSize, $uploadMaxSize);
if ($postMaxSize > 0) {
$min = min($min, $postMaxSize);
}
if ($uploadMaxSize > 0) {
$min = min($min, $uploadMaxSize);
}
self::$_maxFileSize = $min;
}
return self::$_maxFileSize;
}
/**
* Converts a ini setting to a integer value
*
* @deprecated 100.1.0 Please use \Magento\Framework\Convert\DataSize
*
* @param string $size
* @return integer
*/
public function convertSizeToInteger($size)
{
return $this->getDataSize()->convertSizeToBytes($size);
}
/**
* Gets the value of a configuration option
*
* @link http://php.net/manual/en/function.ini-get.php
* @param string $param The configuration option name
* @return string
*/
protected function _iniGet($param)
{
return trim(ini_get($param));
}
/**
* The getter function to get the new dependency for real application code
*
* @return \Magento\Framework\Convert\DataSize
*
* @deprecated 100.1.0
*/
private function getDataSize()
{
if ($this->dataSize === null) {
$this->dataSize =
\Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Convert\DataSize::class);
}
return $this->dataSize;
}
}