如何在 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
How to correctly use `.user.ini`-file in PHP?
提问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
请看一下
Also check this question out Loading a .user.ini PHP conf file using the PHP 5.4 internal server?

