Sizes
ByteSize::parse($size) reads a size, as written by a person, and returns it in bytes, or null if it is not a valid size.
use Derafu\File\ByteSize;
ByteSize::parse('2048'); // 2048
ByteSize::parse('2M'); // 2000000
ByteSize::parse('1.5Mi'); // 1572864
ByteSize::parse('2MB'); // null
It is a whole number of bytes, or a number with a unit that can have decimals with a point. The units are not case sensitive:
| Unit | Bytes |
|---|---|
k, m, g |
Decimal: 1000, 10002, 10003. |
ki, mi, gi |
Binary: 1024, 10242, 10243. |
The result is rounded down to a whole byte (1.1ki is 1126). The calculation is done with integers, so the rounding does not depend on how a float is stored.
Nothing else is a size: a unit alone (M), a number with spaces, a negative number or one with a sign, a decimal without a unit (1.5), a decimal point without decimals (1.M), a comma as the separator, an exponent, a unit that is not in the list (MB, B), a number with more than 9 decimals (without the zeros at the end), or a size that does not fit in an integer.