Question

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.

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.

Was it helpful?

Solution

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.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"

OTHER TIPS

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

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

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";
?>

Try configuring PHP via Apache config files:

PHP_admin_value upload_tmp_dir $SCRATCH_HOME/uploads

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top