@@ -70,17 +70,39 @@ public function getSize()
7070 return $ this ->size ?? ($ this ->size = parent ::getSize ());
7171 }
7272
73+ /**
74+ * Retrieve the file size by unit, calculated in IEC standards with 1024 as base value.
75+ *
76+ * @return false|int|string
77+ */
78+ public function getSizeByUnitBinary (FileSizeUnit $ unit = FileSizeUnit::B, int $ precision = 3 )
79+ {
80+ return $ this ->getSizeByUnitInternal (1024 , $ unit , $ precision );
81+ }
82+
83+ /**
84+ * Retrieve the file size by unit, calculated in metric standards with 1000 as base value.
85+ *
86+ * @return false|int|string
87+ */
88+ public function getSizeByUnitMetric (FileSizeUnit $ unit = FileSizeUnit::B, int $ precision = 3 )
89+ {
90+ return $ this ->getSizeByUnitInternal (1000 , $ unit , $ precision );
91+ }
92+
7393 /**
7494 * Retrieve the file size by unit.
7595 *
96+ * @deprecated Use getSizeByUnitBinary or getSizeByUnitMetric instead
97+ *
7698 * @return false|int|string
7799 */
78100 public function getSizeByUnit (string $ unit = 'b ' )
79101 {
80102 return match (strtolower ($ unit )) {
81- 'kb ' => number_format ( $ this ->getSize () / 1024 , 3 ),
82- 'mb ' => number_format (( $ this ->getSize () / 1024 ) / 1024 , 3 ),
83- default => $ this ->getSize (),
103+ 'kb ' => $ this ->getSizeByUnitBinary (FileSizeUnit:: KB ),
104+ 'mb ' => $ this ->getSizeByUnitBinary (FileSizeUnit:: MB ),
105+ default => $ this ->getSizeByUnitBinary (FileSizeUnit::B)
84106 };
85107 }
86108
@@ -189,4 +211,19 @@ public function getDestination(string $destination, string $delimiter = '_', int
189211
190212 return $ destination ;
191213 }
214+
215+ protected function getSizeByUnitInternal (int $ fileSizeBase , FileSizeUnit $ unit , int $ precision )
216+ {
217+ $ exponent = $ unit ->value ;
218+ $ divider = pow ($ fileSizeBase , $ exponent );
219+
220+ $ size = $ this ->getSize () / $ divider ;
221+
222+ if ($ unit !== FileSizeUnit::B)
223+ {
224+ $ size = number_format ($ size , $ precision );
225+ }
226+
227+ return $ size ;
228+ }
192229}
0 commit comments