0

My server is Linux shared hosting which allow me to specify .user.ini for php initialization. I am try to log error by throwing an Exception. It work when i specify the path as "./logs/phperrors.log" but not with the ~/logs/phperrors.log which refer to my home directory.

error_log = "~/logs/phperrors.log"

file_uploads = On
post_max_size = 1024M
upload_max_filesize = 1024M
max_input_time = -1
max_execution_time = -1
memory_limit = -1
max_file_uploads = 1000
Kent Liau
  • 915
  • 15
  • 26

3 Answers3

2

Yes. PHP ini strings can contain environment variables.

This is a note from the include_path ini setting description:

ENV variables are also accessible in .ini files. As such it is possible to reference the home directory using ${LOGIN} and ${USER}.
Environment variables may vary between Server APIs as those environments may be different.

In your case the environment variable you're looking for is most likely then written as ${HOME}:

error_log = "${HOME}/logs/phperrors.log"
hakre
  • 193,403
  • 52
  • 435
  • 836
0

One thought would be to use ini_set since there is not an easy way to do what you want to do directly in the .ini file.

First, check to see if HOME is in your server environment variable... if it is, use it, if not, maybe you could have a config file holding something hard coded as a default.

    if (getenv("HOME")) {
        $home = getenv("HOME");
        ini_set('error_log', $home . "/logs/phperrors.log");
    } else {
        ini_set('error_log', $config['log_directory']);
    }
John Shipp
  • 1,123
  • 11
  • 21
-1

Sorry not the same syntax - in PHP the tilde is used as a bitwise negation operator.

From the PHP manual https://php.net/manual/en/language.operators.bitwise.php

Bitwise Operators enter image description here

On a Lunix machine instead use

$_SERVER['HOME']

in other words

error_log = $_SERVER['HOME'] . "/logs/phperrors.log"

See full answer to similar question: How to get the home directory from a PHP CLI script?

Regarding how to modify you php.ini
I haven't tried it but this should work - in one of your PHP files add the code:

ini_set('error_log', $_SERVER['HOME'] . '/logs/phperrors.log');

reference: http://www.php.net/manual/en/function.ini-set.php

Community
  • 1
  • 1
TheSteven
  • 900
  • 8
  • 23