PHP 5.3.3 中的 ini_set("memory_limit") 根本不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5061917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ini_set("memory_limit") in PHP 5.3.3 is not working at all
提问by Ant
I had this working before :
我以前有这个工作:
echo ini_get("memory_limit")."\n";
ini_set("memory_limit","256M");
echo ini_get("memory_limit")."\n";
That would input this :
那将输入这个:
32M
256M
on a php script executed by command line. I updated from 5.2 to 5.3, and from now, this directive is not working at all : this gives me :
在由命令行执行的 php 脚本上。我从 5.2 更新到 5.3,从现在开始,这个指令根本不起作用:这给了我:
32M
32M
and then make my script fail with a fatal Error...
然后使我的脚本因致命错误而失败...
I checked the php documentation, and googled it, and I didn't find anywhere that "memory_limit" had been deprecated.
我检查了 php 文档,并用谷歌搜索了它,我没有找到“memory_limit”已被弃用的任何地方。
Does anyone have a solution?
有没有人有办法解决吗?
采纳答案by Wouter van Eekelen
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).
很可能您的 suhosin 已更新,这将 suhosin.memory_limit 的默认值从禁用更改为 0(这将不允许对 memory_limit 进行任何更新)。
On Debian, change /etc/php5/conf.d/suhosin.ini
在 Debian 上,更改 /etc/php5/conf.d/suhosin.ini
;suhosin.memory_limit = 0
;suhosin.memory_limit = 0
to
到
suhosin.memory_limit = 2G
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:
或者任何你觉得舒服的值。你可以在http://www.hardened-php.net/hphp/changelog.html找到 Susosin 的更新日志,它说:
Changed the way the memory_limit protection is implemented
改变了 memory_limit 保护的实现方式
回答by Oscar M.
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.
如果您启用了 suhosin 扩展,它可以防止脚本设置超出它开始的内存限制或某些定义的上限。
http://www.hardened-php.net/suhosin/configuration.html#suhosin.memory_limit
http://www.hardened-php.net/suhosin/configuration.html#suhosin.memory_limit
回答by Carlton
Here's a list of things that are worth checking:
以下是值得检查的事项列表:
Is Suhosin installed?
是否安装了Suhosin?
ini_set
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
- 格式很重要
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
http://php.net/manual/en/ini.core.php#ini.memory-limit
- Has php_admin_value been used in .htaccess or virtualhost files?
- php_admin_value 是否在 .htaccess 或 virtualhost 文件中使用过?
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.
设置指定指令的值。这不能在 .htaccess 文件中使用。任何使用 php_admin_value 设置的指令类型都不能被 .htaccess 或 ini_set() 覆盖。要清除先前设置的值,请使用 none 作为值。
回答by Appmerce
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:
Ubuntu 10.04 只带有 Suhosin 补丁,它没有给你配置选项。但是你可以安装 php5-suhosin 来解决这个问题:
apt-get update
apt-get install php5-suhosin
Now you can edit /etc/php5/conf.d/suhosin.ini and set:
现在您可以编辑 /etc/php5/conf.d/suhosin.ini 并设置:
suhosin.memory_limit = 1G
Then using ini_set will work in a script:
然后使用 ini_set 将在脚本中工作:
ini_set('memory_limit', '256M');
回答by mario
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).
对我有用,与 PHP 5.3 无关。就像许多这样的选项一样,ini_set()
当启用 safe_mode 时,它不能被覆盖。检查您的更新php.ini
(更好的是:也更改 memory_limit 那里)。
回答by Lechampi
Let's do a test with 2 examples:
让我们用 2 个例子做一个测试:
<?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:
上面的示例不适用于覆盖 memory_limit 值。但这会起作用:
<?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 fileor at least before any echo.
您必须将 放在文件ini_set('memory_limit','128M');
的顶部或至少在任何 echo 之前。
As for me, suhosinwasn't the solution because it doesn't even appear in my phpinfo(), but this worked:
至于我,suhosin不是解决方案,因为它甚至没有出现在我的 phpinfo() 中,但这有效:
<?php
ini_set('memory_limit','2048M'); // set at the top of the file
(...)
?>