我如何检查 ini_set 是否可以在服务器上工作?

发布于 2024-12-21 19:43:30 字数 211 浏览 2 评论 0原文

设置如下选项

ini_set('upload_max_filesize', '8M');

我如何检查服务器配置是否允许我在 PHP 脚本中 ?这是 php.ini 指令 的列表,但我真的不知道如何制作在尝试更改该值之前进行检查。

How can i check if server configuration allows me to set an option like:

ini_set('upload_max_filesize', '8M');

in a PHP script? Here is a list of php.ini directives but i can't really figure out how to make a check before tring to change that value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

强者自强 2024-12-28 19:43:30

检查我是否可以对某些选项使用ini_set,如何?

ini_set 将返回旧的成功时值为,失败时为 false*。有了这些知识,您可以编写一条语句来检查您的呼叫是否成功,如下所示。

$result = ini_set ("some_option", "some_value");
$failure = (version_compare(PHP_VERSION, '5.3.0') >= 0) ? false : '';
if ($result === $failure)
   echo "Unable to use ini_set to change 'some_option'!";

(*):请注意 PHP 5.3.0 中的返回值从 ''(空字符串)更改为 false。因此,您还需要检查当前的 PHP 版本。


另一种方法是使用 ini_get_all< /a> 将为您提供有关每个可用选项的详细信息,它是 使用权级别

$all_option_details = ini_get_all ();

/* see the comments in this post regarding PHP_INI_USER vs INI_USER
 * seems like someone writing the relevant PHP documentation fcuked up
 *
 * props to @soulmerge */

if ($all_option_details['upload_max_filesize']['access'] & INI_USER)
   echo "we are allowed to change upload_max_filesize from with ini_set!";

我想禁用某些选项的 ini_set,如何操作?

有几种方法可以使选项在运行时不可更改(以禁用 ini_set 的方式),其中包括以下两种方法,您可以在提供的链接中阅读更多信息。

php_admin_value 名称值

设置指定指令的值。这不能在 .htaccess 文件中使用。使用 php_admin_value 设置的任何指令类型都不能被 .htaccess 或 ini_set() 覆盖。要清除先前设置的值,请​​使用 none 作为值。

 

php_admin_flag 名称开|关

用于设置布尔配置指令。这不能在 .htaccess 文件中使用。使用 php_admin_flag 设置的任何指令类型都不能被 .htaccess 或 ini_set() 覆盖


示例(取自文档)

<IfModule mod_php5.c>
  php_value include_path ".:/usr/local/lib/php"
  php_admin_flag engine on
</IfModule>

<IfModule mod_php4.c>
  php_value include_path ".:/usr/local/lib/php"
  php_admin_flag engine on
</IfModule>

Check if I'm allowed to use ini_set for some option, how?

ini_set will return the old value on success, and false* on failure. With this knowledge you can write a statement checking if your call went through, like the below.

$result = ini_set ("some_option", "some_value");
$failure = (version_compare(PHP_VERSION, '5.3.0') >= 0) ? false : '';
if ($result === $failure)
   echo "Unable to use ini_set to change 'some_option'!";

(*): Note the return value changed in PHP 5.3.0 from '' (an empty string) to false. So you need to check your current PHP version as well.


Another method is to use ini_get_all which will provide you with details regarding every option available, and it's access level.

$all_option_details = ini_get_all ();

/* see the comments in this post regarding PHP_INI_USER vs INI_USER
 * seems like someone writing the relevant PHP documentation fcuked up
 *
 * props to @soulmerge */

if ($all_option_details['upload_max_filesize']['access'] & INI_USER)
   echo "we are allowed to change upload_max_filesize from with ini_set!";

I'd like to disable the use of ini_set for some option(s), how?

There are a few methods of making options unchangeable runtime (in a way disabling ini_set), among them are the following two which you can read more about at the provided link.

php_admin_value name value

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.

php_admin_flag name on|off

Used to set a boolean configuration directive. This can not be used in .htaccess files. Any directive type set with php_admin_flag can not be overridden by .htaccess or ini_set().


Example (taken from this documentation)

<IfModule mod_php5.c>
  php_value include_path ".:/usr/local/lib/php"
  php_admin_flag engine on
</IfModule>

<IfModule mod_php4.c>
  php_value include_path ".:/usr/local/lib/php"
  php_admin_flag engine on
</IfModule>
无需解释 2024-12-28 19:43:30

除非您尝试设置所需的值,否则不可能知道您的真正限制是多少。例如,可能安装了 suhosin 补丁,这可能会阻止您更改以下值全部。

因此,检查是否可行的唯一选择是尝试并检查返回值(在任何情况下都应该这样做):

$oldValue = ini_get('upload_max_filesize');
if (ini_set('upload_max_filesize', '8M') === false) {
    die("Couldn't update upload file size.");
}
if (ini_set('upload_max_filesize', $oldValue) === false) {
    die("Error resetting upload file size.");
}
// you can safely assume that it is possible to set
// upload_max_filesize to 8M from this line onward.

It is impossible to know what your real limit is unless you try to set the desired value. There might be the suhosin patch installed, for example, which could prevent you from changing the value at all.

So your only option for checking if it is possible is to try it and check the return value (which you should to in any case):

$oldValue = ini_get('upload_max_filesize');
if (ini_set('upload_max_filesize', '8M') === false) {
    die("Couldn't update upload file size.");
}
if (ini_set('upload_max_filesize', $oldValue) === false) {
    die("Error resetting upload file size.");
}
// you can safely assume that it is possible to set
// upload_max_filesize to 8M from this line onward.
停滞 2024-12-28 19:43:30
echo ini_get (<BLA>);
ini_set (<BLA>);
echo ini_get (<BLA>);
echo ini_get (<BLA>);
ini_set (<BLA>);
echo ini_get (<BLA>);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文