Is it possible to use environment variables in php.ini?
According to the PHP docs, environment-variables can indeed be used in the configuration file.
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:
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";
?>
Documented officially: https://www.php.net/manual/en/configuration.file.php#example-36
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.conf
and php.ini
file
Note: I will be omitting some text for brevity.
In the httpd.conf
file
# 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.ini
file
; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = "${PHP_HOME}/ext"