Skip to content

Commit

Permalink
Merge pull request #5602 from WoltLab/ini-parse-quantity
Browse files Browse the repository at this point in the history
Make use of `ini_parse_quantity()` in FileUtil::getMemoryLimit()
  • Loading branch information
TimWolla authored Aug 3, 2023
2 parents 0245b66 + ef11727 commit 6732d38
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions wcfsetup/install/files/lib/util/FileUtil.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,29 +514,31 @@ public static function getMemoryLimit(): int
$memoryLimit = \ini_get('memory_limit');

// no limit
if ($memoryLimit == -1) {
if ($memoryLimit == "-1") {
self::$memoryLimit = -1;
}

// completely numeric, PHP assumes byte
if (\is_numeric($memoryLimit)) {
self::$memoryLimit = $memoryLimit;
}
} else if (\function_exists('ini_parse_quantity')) {
self::$memoryLimit = \ini_parse_quantity($memoryLimit);
} else {
// completely numeric, PHP assumes byte
if (\is_numeric($memoryLimit)) {
self::$memoryLimit = $memoryLimit;
}

// PHP supports 'K', 'M' and 'G' shorthand notation
if (\preg_match('~^(\d+)\s*([KMG])$~i', $memoryLimit, $matches)) {
switch (\strtoupper($matches[2])) {
case 'K':
self::$memoryLimit = $matches[1] * 1024;
break;
// PHP supports 'K', 'M' and 'G' shorthand notation
if (\preg_match('~^(\d+)\s*([KMG])$~i', $memoryLimit, $matches)) {
switch (\strtoupper($matches[2])) {
case 'K':
self::$memoryLimit = $matches[1] * 1024;
break;

case 'M':
self::$memoryLimit = $matches[1] * 1024 * 1024;
break;
case 'M':
self::$memoryLimit = $matches[1] * 1024 * 1024;
break;

case 'G':
self::$memoryLimit = $matches[1] * 1024 * 1024 * 1024;
break;
case 'G':
self::$memoryLimit = $matches[1] * 1024 * 1024 * 1024;
break;
}
}
}
}
Expand Down

0 comments on commit 6732d38

Please sign in to comment.