checking memory_limit in PHP

后端 未结 9 1524
臣服心动
臣服心动 2020-12-29 01:40

I\'m need to check if memory_limit is at least 64M in my script installer. This is just part of PHP code that should work, but probably due to this

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 02:18

    Here is another simpler way to check that.

    $memory_limit = return_bytes(ini_get('memory_limit'));
    if ($memory_limit < (64 * 1024 * 1024)) {
        // Memory insufficient      
    }
    
    /**
    * Converts shorthand memory notation value to bytes
    * From http://php.net/manual/en/function.ini-get.php
    *
    * @param $val Memory size shorthand notation string
    */
    function return_bytes($val) {
        $val = trim($val);
        $last = strtolower($val[strlen($val)-1]);
        $val = substr($val, 0, -1);
        switch($last) {
            // The 'G' modifier is available since PHP 5.1.0
            case 'g':
                $val *= 1024;
            case 'm':
                $val *= 1024;
            case 'k':
                $val *= 1024;
        }
        return $val;
    }
    

提交回复
热议问题