ini_set(“memory_limit”) in PHP 5.3.3 is not working at all

前端 未结 6 1171
长发绾君心
长发绾君心 2020-11-28 10:57

I had this working before :

echo ini_get(\"memory_limit\").\"\\n\";
ini_set(\"memory_limit\",\"256M\");
echo ini_get(\"memory_limit\").\"\\n\";
相关标签:
6条回答
  • 2020-11-28 11:28

    Let's do a test with 2 examples:

        <?php    
        $memory = (int)ini_get("memory_limit"); // Display your current value in php.ini (for example: 64M)
        echo "original memory: ".$memory."<br>";
        ini_set('memory_limit','128M'); // Try to override the memory limit for this script
        echo "new memory:".$memory;
        }
        // Will display:
        // original memory: 64
        // new memory: 64
        ?>
    

    The above example doesn't work for overriding the memory_limit value. But This will work:

        <?php
        $memory = (int)ini_get("memory_limit"); // get the current value
        ini_set('memory_limit','128'); // override the value
        echo "original memory: ".$memory."<br>"; // echo the original value
        $new_memory = (int)ini_get("memory_limit"); // get the new value
        echo "new memory: ".$new_memory;  // echo the new value
        // Will display:
        // original memory: 64
        // new memory: 128
        ?>
    

    You have to place the ini_set('memory_limit','128M'); at the top of the file or at least before any echo.

    As for me, suhosin wasn't the solution because it doesn't even appear in my phpinfo(), but this worked:

        <?php
        ini_set('memory_limit','2048M'); // set at the top of the file
        (...)
        ?>
    
    0 讨论(0)
  • 2020-11-28 11:29

    If you have the suhosin extension enabled, it can prevent scripts from setting the memory limit beyond what it started with or some defined cap.

    http://www.hardened-php.net/suhosin/configuration.html#suhosin.memory_limit

    0 讨论(0)
  • 2020-11-28 11:34

    Here's a list of things that are worth checking:

    Is Suhosin installed?

    • How to check whether Suhosin is installed?

    ini_set

    • The format is important ini_set('memory_limit', '512'); // DIDN'T WORK ini_set('memory_limit', '512MB'); // DIDN'T WORK ini_set('memory_limit', '512M'); // OK - 512MB ini_set('memory_limit', 512000000); // OK - 512MB

    When an integer is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used.

    http://php.net/manual/en/ini.core.php#ini.memory-limit

    • Has php_admin_value been used in .htaccess or virtualhost files?

    Sets the value of the specified directive. This can not be used in .htaccess files. Any directive type set with php_admin_value can not be overridden by .htaccess or ini_set(). To clear a previously set value use none as the value.

    http://php.net/manual/en/configuration.changes.php

    0 讨论(0)
  • 2020-11-28 11:34

    Works for me, has nothing to do with PHP 5.3. Just like many such options it cannot be overriden via ini_set() when safe_mode is enabled. Check your updated php.ini (and better yet: change the memory_limit there too).

    0 讨论(0)
  • 2020-11-28 11:39

    Most likely your sushosin updated, which changed the default of suhosin.memory_limit from disabled to 0 (which won't allow any updates to memory_limit).

    On Debian, change /etc/php5/conf.d/suhosin.ini

    ;suhosin.memory_limit = 0

    to

    suhosin.memory_limit = 2G

    Or whichever value you are comfortable with. You can find the changelog of Sushosin at http://www.hardened-php.net/hphp/changelog.html, which says:

    Changed the way the memory_limit protection is implemented

    0 讨论(0)
  • 2020-11-28 11:45

    Ubuntu 10.04 comes with the Suhosin patch only, which does not give you configuration options. But you can install php5-suhosin to solve this:

    apt-get update
    apt-get install php5-suhosin
    

    Now you can edit /etc/php5/conf.d/suhosin.ini and set:

    suhosin.memory_limit = 1G
    

    Then using ini_set will work in a script:

    ini_set('memory_limit', '256M');
    
    0 讨论(0)
提交回复
热议问题