是否可以在 php.ini 中使用环境变量?

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

Is it possible to use environment variables in php.ini?

phpconfigurationenvironment-variables

提问by Joe Holloway

Rather than hard-wiring some paths in my php.ini configuration, I'd like to configure them using system variables that are shared in some other places such as my Apache configuration. I've done some searching and couldn't find the right mix of keywords to discover if there's a way to do this.

我不想在我的 php.ini 配置中硬连接一些路径,而是想使用在其他一些地方(例如我的 Apache 配置)中共享的系统变量来配置它们。我已经做了一些搜索,但找不到正确的关键字组合来发现是否有办法做到这一点。

Does anyone know if this can be done?

有谁知道这是否可以做到?

upload_tmp_dir = $SCRATCH_HOME/uploads

Now SCRATCH_HOME can be exported in the environment as /tmp or /var/scratch or whatever I want it to be.

现在 SCRATCH_HOME 可以在环境中导出为 /tmp 或 /var/scratch 或我想要的任何内容。

回答by Hlulani

This feature is not well documented. Before I begin, I just want to specify my configurations: I'm using Windows 7-64bit, PHP 5.4.3, Apache HTTP Server 2.2.x, I've set my environmental variable PHP_HOME=C:\tools\php-5.4.3(PHP installation directory). I use the variable in my httpd.confand php.inifile

此功能没有很好的文档记录。在开始之前,我只想指定我的配置:我使用的是 Windows 7-64 位、PHP 5.4.3、Apache HTTP Server 2.2.x,我已经设置了我的环境变量PHP_HOME=C:\tools\php-5.4.3(PHP 安装目录)。我在我的httpd.confphp.ini文件中使用变量

Note: I will be omitting some text for brevity.

注意:为简洁起见,我将省略一些文字。

In the httpd.conffile

httpd.conf文件中

# For PHP 5 do something like this:
LoadModule php5_module "${PHP_HOME}/php5apache2_2.dll"
AddType application/x-httpd-php .php

# configure the path to php.ini
PHPIniDir "${PHP_HOME}"

In the php.inifile

php.ini文件中

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = "${PHP_HOME}/ext"

回答by Synetech

According to the PHP docs, environment-variables can indeed be used in the configuration file.

根据PHP docs,环境变量确实可以在配置文件中使用。

Screenshot of php docs with URL and note about envvars being allowed in PHP.ini

带有 URL 的 php 文档屏幕截图,并注意 PHP.ini 中允许使用的 envvars

It doesn't say anything about what syntax to use, but it is the same as with Apache's configuration file which uses *nix syntax. So for example, if you want PHP to use the system temp-directory, you would use this:

它没有说明使用什么语法,但它与使用 *nix 语法的 Apache 配置文件相同。例如,如果您希望 PHP 使用系统临时目录,您可以使用以下命令:

upload_tmp_dir = ${Temp}

You can confirm that it is active with the following script:

您可以使用以下脚本确认它处于活动状态:

<?php
    echo "ini:  " . ini_get('upload_tmp_dir') . "\n";
    echo "env:  " . sys_get_temp_dir()        . "\n";
    echo "temp: " . getenv('temp')            . "\n";
    echo "tmp:  " . getenv('tmp')             . "\n";
?>

回答by István Palócz

You can use it, and it is well documented now:

你可以使用它,它现在有据可查:

https://www.php.net/manual/en/configuration.file.php#example-36

https://www.php.net/manual/en/configuration.file.php#example-36

回答by Maciej ?ebkowski

Try configuring PHP via Apache config files:

尝试通过Apache 配置文件配置 PHP :

PHP_admin_value upload_tmp_dir $SCRATCH_HOME/uploads

Works fine for me. (psst, you cannot change upload_tmp_dirusing .htaccess)

对我来说很好用。(psst,您不能upload_tmp_dir使用 .htaccess 进行更改)