单个文件夹或模块的内存限制

时间:2013-11-13 15:04:15

标签: php codeigniter memory-management hmvc memory-limit

我正在使用带有hmvc库的codeigniter框架开发一个php应用程序,应用程序将有模块,我希望每个模块都有不同的内存限制。这可能吗?

1 个答案:

答案 0 :(得分:2)

您可以使用内存限制配置向每个模块的根添加.htaccess

<IfModule mod_php5.c>
    php_value memory_limit 32M
</IfModule>

要动态更新文件,您需要考虑.htaccess是一个危险的事情。

我会非常努力地确保没有办法允许用户输入(即使是你的,因为如果你能做到,其他人也可以这样做)就可以写入.htaccess文件。

那说,伪代码示例(告诫者):

private function _set_mem_limit($limit = 0, $module = '')
{
    //modules
    $available_modules = array(
        'module1' => '/var/www/myApp/modules/module1/',
        'module2' => '/var/www/myApp/modules/module2/',
    );

    //limits
    $available_limits = array(
        '32'    => '32M',
        '64'    => '64M',
        '128'   => '128M',
        '256'   => '256M',
        '512'   => '512M'
    );

    $string = 'php_value memory_limit ';

    // make sure limit is an integer and it is in the available_limits array
    // if so, set string with the selected limit from array. otherwise, exit
    if(gettype($limit) === 'integer' and in_array($limit, $available_limits))
    {
        $string .= $available_limits[$limit];
    }
    else
    {
        die('Problem setting mem limit.');
    }

    // make sure incoming module variable is only alphanumeric and exists in
    // available_modules array. if so, write the limit to the file. otherwise, exit
    if(ctype_alpha($module) and in_array($module, $available_modules))
    {
        $path = $available_modules[$module].'.htaccess';
        $f = fopen($path, 'w');
        fwrite($f, $string);
        fclose($f);

    }
    else
    {
        die('Problem setting path for mem limit.');
    }
}

尽管这可行,但我不相信。

相关问题