public static function valueToBytes($value): int
    {
        if (is_int($value)) {
            return $value;
        }

        $value = trim($value);

        // Native PHP check for digits in string
        if (ctype_digit(ltrim($value, '-'))) {
            return (int)$value;
        }

        $signed = (substr($value, 0, 1) === '-') ? -1 : 1;

        // Kilobytes
        if (preg_match('/(\d+)K$/i', $value, $matches)) {
            return (int)($matches[1] * $signed * 1024);
        }

        // Megabytes
        if (preg_match('/(\d+)M$/i', $value, $matches)) {
            return (int)($matches[1] * $signed * 1024 * 1024);
        }

        // Gigabytes
        if (preg_match('/(\d+)G$/i', $value, $matches)) {
            return (int)($matches[1] * $signed * 1024 * 1024 * 1024);
        }

        throw new InvalidArgumentException("Failed to find K, M, or G in a non-integer value [$value]");
    }
public static function valueToBytes($value): int
    {
        if (is_int($value)) {
            return $value;
        }

        $value = trim($value);

        // Native PHP check for digits in string
        if (ctype_digit(ltrim($value, '-'))) {
            return (int)$value;
        }

        $signed = (substr($value, 0, 1) === '-') ? -1 : 1;

        // Kilobytes
        if (preg_match('/(\d+)K$/i', $value, $matches)) {
            return (int)($matches[1] * $signed * 1024);
        }

        // Megabytes
        if (preg_match('/(\d+)M$/i', $value, $matches)) {
            return (int)($matches[1] * $signed * 1024 * 1024);
        }

        // Gigabytes
        if (preg_match('/(\d+)G$/i', $value, $matches)) {
            return (int)($matches[1] * $signed * 1024 * 1024 * 1024);
        }

        throw new InvalidArgumentException("Failed to find K, M, or G in a non-integer value [$value]");
    }
public static function valueToBytes ( $ value ) : int { if ( is_int ( $ value ) ) { return $ value ; } $ value = trim ( $ value ) ; // Native PHP check for digits in string if ( ctype_digit ( ltrim ( $ value , '-' ) ) ) { return ( int ) $ value ; } $ signed = ( substr ( $ value , 0 , 1 ) === '-' ) ? - 1 : 1 ; // Kilobytes if ( preg_match ( '/(\d+)K$/i' , $ value , $ matches ) ) { return ( int ) ( $ matches [ 1 ] * $ signed * 1024 ) ; } // Megabytes if ( preg_match ( '/(\d+)M$/i' , $ value , $ matches ) ) { return ( int ) ( $ matches [ 1 ] * $ signed * 1024 * 1024 ) ; } // Gigabytes if ( preg_match ( '/(\d+)G$/i' , $ value , $ matches ) ) { return ( int ) ( $ matches [ 1 ] * $ signed * 1024 * 1024 * 1024 ) ; } throw new InvalidArgumentException ( "Failed to find K, M, or G in a non-integer value [$value]" ) ; }
Convert value to bytes

Convert sizes from PHP settings like post_max_size
for example 8M to integer number of bytes.

If number is integer return as is.

NOTE: This is a modified copy from qobo/cakephp-utils/config/bootstrap.php

@throws \InvalidArgumentException when cannot convert
@param string|int $value Value to convert
@return int
Convert value to bytes