diff options
author | Paul Arthur <paul.arthur@flowerysong.com> | 2012-07-01 17:05:17 -0400 |
---|---|---|
committer | Paul Arthur <paul.arthur@flowerysong.com> | 2012-07-01 17:09:36 -0400 |
commit | 567b27954b34906d23ad4f71b93533d78c40311f (patch) | |
tree | de37b7696c7881e1309eca19aa97037394a25298 | |
parent | 3a10779e0098fadbbd490a535b0c662a73c6623f (diff) | |
download | ampache-567b27954b34906d23ad4f71b93533d78c40311f.tar.gz ampache-567b27954b34906d23ad4f71b93533d78c40311f.tar.bz2 ampache-567b27954b34906d23ad4f71b93533d78c40311f.zip |
Clean up set_memory_limit
ini_set should be passed an integer byte value; shorthand byte values
should only be used in php.ini. It's bad to assume that values are
always in megabytes; we could enforce it in our config file if we
cared, but not in PHP's.
-rw-r--r-- | lib/general.lib.php | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/lib/general.lib.php b/lib/general.lib.php index fb7ab1a4..d3bf8d02 100644 --- a/lib/general.lib.php +++ b/lib/general.lib.php @@ -33,12 +33,11 @@ */ function set_memory_limit($new_limit) { - $current_memory = ini_get('memory_limit'); - $current_memory = substr($current_memory,0,strlen($current_memory)-1); - if ($current_memory < $new_limit) { - $php_memory = $new_limit . "M"; - ini_set (memory_limit, "$php_memory"); - unset($php_memory); + $current_limit = unformat_bytes(ini_get('memory_limit')); + $new_limit = unformat_bytes($new_limit); + + if ($current_limit < $new_limit) { + ini_set (memory_limit, $new_limit); } } // set_memory_limit @@ -148,11 +147,36 @@ function format_bytes($value, $precision = 2) { case 4: $unit = 'TB'; break; case 5: $unit = 'PB'; break; default: $unit = 'B'; break; - } // end switch + } // end switch return round($value, $precision) . ' ' . $unit; } +function unformat_bytes($value) { + if (preg_match('/^([0-9]+) *([[:alpha:]]+)$/', $value, $matches)) { + $value = $matches[1]; + $unit = strtolower(substr($matches[2], 0, 1)); + } + else { + return $value; + } + + switch($unit) { + case 'p': + $value *= 1024; + case 't': + $value *= 1024; + case 'g': + $value *= 1024; + case 'm': + $value *= 1024; + case 'k': + $value *= 1024; + } + + return $value; +} + /** * make_bool * This takes a value and returns what we consider to be the correct boolean |