如何在 PHP 中正确使用 `.user.ini` 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32179160/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:49:37  来源:igfitidea点击:

How to correctly use `.user.ini`-file in PHP?

phpsettingsini

提问by Edward

In my working directory I have two files: index.phpand .user.ini:

在我的工作目录中,我有两个文件:index.php.user.ini

.user.ini:

.user.ini

display_errors=on

; http://be2.php.net/manual/en/filter.configuration.php#ini.filter.default
;filter.default = "full_special_chars"

index.php:

index.php

<?php
//ini_set("display_errors", "on");

// Use `.user.ini` file:
// http://php.net/manual/en/configuration.file.per-user.php
echo "Setting for 'user_ini.filename': " . ini_get("user_ini.filename");
echo "\n\n";


// It takes up to five minutes, until `.user.ini` is re-read:
echo "Setting for 'user_ini.cache_ttl': " . ini_get("user_ini.cache_ttl");
echo "\n\n";

// http://php.net/manual/en/function.ini-get.php
echo "Setting for 'display_errors': " . ini_get("display_errors");
echo "\n\n";
echo "Setting for 'filter.default': " . ini_get("filter.default");
echo "\n\n";

// php -S localhost:8000
// http://localhost:8000/

Using the above .user.ini-file (in my working directory) I expect the "Setting for 'display_errors': "having a value of onor 1, but it's empty.

使用上面的.user.ini-file(在我的工作目录中),我希望"Setting for 'display_errors': "它的值为onor 1,但它是空的。

How to correctly change settings using the .user.ini-file?

如何使用.user.ini-file正确更改设置?

running php --iniresults in

运行php --ini结果

Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File:         /etc/php5/cli/php.ini
Scan for additional .ini files in: /etc/php5/cli/conf.d
Additional .ini files parsed:      /etc/php5/cli/conf.d/05-opcache.ini,
/etc/php5/cli/conf.d/10-pdo.ini,
/etc/php5/cli/conf.d/20-json.ini,
/etc/php5/cli/conf.d/20-readline.ini

which does not contain my .user.ini-file.

其中不包含我的.user.ini-file。

Explicitly adding the .user.ini-file works:

显式添加.user.ini-file 有效:

php --php-ini .user.ini index.php

but I'd like it to be automatically read when running the script from a given folder.

但我希望在从给定文件夹运行脚本时自动读取它。

回答by R_User

In the documentationit says:

文档中它说:

These files are processed only by the CGI/FastCGI SAPI

这些文件仅由 CGI/FastCGI SAPI 处理

and

If you are using Apache, use .htaccessfiles for the same effect.

如果您使用的是 Apache,请使用.htaccess文件来达到相同的效果。

So if you run PHP as an Apache module, from the commandline, or using the builtin-Server, the .user.ini-files are not processed.

因此,如果您将 PHP 作为 Apache 模块、从命令行或使用内置服务器运行,.user.ini则不会处理 -files。

To find out your Server API, simply generate a PHP script with the following content and call it in your webbrowser:

要找到您的服务器 API,只需生成一个包含以下内容的 PHP 脚本并在您的网络浏览器中调用它:

<? phpinfo();

回答by Alex Andrei

According to http://php.net/manual/en/configuration.file.per-user.php

根据http://php.net/manual/en/configuration.file.per-user.php

Check user_ini.filenamein your main php.inito make sure it's not blank and it is indeed parsed. If its value is blank then PHP doesn't scan at all.

检查user_ini.filename您的主要内容php.ini以确保它不是空白并且确实已解析。如果它的值为空,则 PHP 根本不扫描。

Also see the value of user_ini.cache_ttl

还可以看到值 user_ini.cache_ttl

Please take a look at

请看一下

php.net manual

php.net 手册

Also check this question out Loading a .user.ini PHP conf file using the PHP 5.4 internal server?

还要检查这个问题 使用 PHP 5.4 内部服务器加载 .user.ini PHP conf 文件?