checking memory_limit in PHP

后端 未结 9 1471
臣服心动
臣服心动 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:20

    Try to convert the value first (eg: 64M -> 64 * 1024 * 1024). After that, do comparison and print the result.

     nnn MB
        } else if ($matches[2] == 'K') {
            $memory_limit = $matches[1] * 1024; // nnnK -> nnn KB
        }
    }
    
    $ok = ($memory_limit >= 64 * 1024 * 1024); // at least 64M?
    
    echo '';
    echo '' . $memory_limit . '';
    echo '' . ($ok ? 1 : 0) . '';
    echo '';
    

    Please note that the above code is just an idea. Don't forget to handle -1 (no memory limit), integer-only value (value in bytes), G (value in gigabytes), k/m/g (value in kilobytes, megabytes, gigabytes because shorthand is case-insensitive), etc.

提交回复
热议问题