16

In my working directory I have two files: index.php and .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:

<?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 on or 1, but it's empty.

How to correctly change settings using the .user.ini-file?

running php --ini results in

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.

Explicitly adding the .user.ini-file works:

php --php-ini .user.ini index.php

but I'd like it to be automatically read when running the script from a given folder.

Edward
  • 4,453
  • 8
  • 44
  • 82

3 Answers3

23

In the documentation it says:

These files are processed only by the CGI/FastCGI SAPI

and

If you are using Apache, use .htaccess files for the same effect.

So if you run PHP as an Apache module, from the commandline, or using the builtin-Server, the .user.ini-files are not processed.

To find out your Server API, simply generate a PHP script with the following content and call it in your webbrowser:

<? phpinfo();
R_User
  • 10,682
  • 25
  • 79
  • 120
  • Is there a PHP-function that returns the Server-API? – Edward Aug 25 '15 at 08:25
  • 1
    @Edward You can use [`php_sapi_name()`](http://php.net/manual/en/function.php-sapi-name.php). – R_User Aug 25 '15 at 19:33
  • 2
    @CMCDragonkai yes, but only if your local server is configured as FastCGI module. You can recognize it by checking phpinfo(); in row `Server API`. – Arxeiss Aug 09 '17 at 11:33
  • @Arxeiss My tests showed that it doesn't work for `php -S`. – CMCDragonkai Aug 10 '17 at 04:16
  • Somehow I skimmed the docs and missed this multiple times when trying to use .user.ini with `php -S`. Thanks for making it obvious for me. Additionally, I found it also does not use /etc/php-cli.ini. After checking `php_sapi_name()`, I was successful with /etc/php-cli-server.ini – Pete Jan 08 '21 at 20:26
4

According to http://php.net/manual/en/configuration.file.per-user.php

Check user_ini.filename in your main php.ini to make sure it's not blank and it is indeed parsed. If its value is blank then PHP doesn't scan at all.

Also see the value of user_ini.cache_ttl

Please take a look at

php.net manual

Also check this question out Loading a .user.ini PHP conf file using the PHP 5.4 internal server?

Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
  • Thats what the `index.php` already does. The output is: `Setting for 'user_ini.filename': .user.ini` and `Setting for 'user_ini.cache_ttl': 300`. So at least after five minutes the correct settings should be returned by the `index.php` - but they aren't,... – Edward Aug 24 '15 at 09:59
1

instead of use .htaccess for php config , u can create .user.ini file and put setting (without any indent or space) . for example my .user.ini config is:

max_execution_time =600
upload_max_filesize=200M
post_max_size=200M
memory_limit=256M 
fatemeh sadeghi
  • 1,757
  • 1
  • 11
  • 14
  • 1
    Are you sure spaces will not work? Also please note there is a space in your .user.ini (see first line max_execution_time =600) – bg17aw Feb 13 '23 at 17:40