Does the .user.ini file work for subdirectories?
Asked Answered
O

2

9

Does the .user.ini file that controls folder specific PHP settings also descend into the subfolders?

I was reading a few websites and they suggest that it does (albeit there is not alot of information about it), however I've found that if I run a script from a subfolder, it doesn't use the settings from the .user.ini file.

Am I missing something or is it only meant to be for the same folder that the script is executing from? If so, is there a way to make php scripts look for the .user.ini file from the parent folder etc?

Oly answered 1/12, 2014 at 10:10 Comment(0)
C
15

Yes, it should work. However, I had the same issue with .user.ini files not setting php_value's recursively. According to official (and short) documentation on php.net they should work recursively (as .htaccess did):

PHP scans for INI files in each directory, starting with the directory of the requested PHP file, and working its way up to the current document root (as set in $_SERVER['DOCUMENT_ROOT']). In case the PHP file is outside the document root, only its directory is scanned.

What I have found out is that Apache configuration had one trailing slash too much which caused .user.ini files not to work recursively.

Take a look at your phpinfo(), specifically SCRIPT_FILENAME variable. Notice two slashes - where should be just one:

$_SERVER['SCRIPT_FILENAME'] = //home/site/public_html/phpnfo.php

The reason for this was coming from apache config, which contained one trailing slash too much.

<IfModule !mod_php5.c>
    <FilesMatch \.php$>
            SetHandler "proxy:unix:/var/lib/php/php-fpm.sockets/site.sock|fcgi://localhost/"
    </FilesMatch>
    DirectoryIndex index.php index.html index.htm
</IfModule>

Apache config doesn't include trailing slashes for directories so instead of fcgi://localhost/ this should be written as fcgi://localhost like this:

<IfModule !mod_php5.c>
    <FilesMatch \.php$>
            SetHandler "proxy:unix:/var/lib/php/php-fpm.sockets/site.sock|fcgi://localhost"
    </FilesMatch>
    DirectoryIndex index.php index.html index.htm
</IfModule>

After change, restart Apache/php-fpm and you are set.

Update: As it turns out, trailing slash errors in Apache config are still common thing and can lead to different errors and bad php practices (eg set in DocumentRoot /var/www/web/ ).

Creech answered 26/1, 2017 at 17:24 Comment(4)
You've just saved me from such a headache. I've been troubleshooting this issue for half a year!Tartaglia
glad I could help :)Creech
You're a hero! Didn't catch that trailing slash, it works now!Baby
For some reason (I don't know) this problem exists on Windows PHP but was fixed in PHP 7.3.19 bugs.php.net/bug.php?id=69436 Thank you.Preshrunk
R
-2

Instead of reading "few websites" I would sugest reading PHP manual:

Since PHP 5.3.0, PHP includes support for .htaccess-style INI files on a per-directory basis. These files are processed only by the CGI/FastCGI SAPI. This functionality obsoletes the PECL htscanner extension. If you are using Apache, use .htaccess files for the same effect.

In addition to the main php.ini file, PHP scans for INI files in each directory, starting with the directory of the requested PHP file, and working its way up to the current document root (as set in $_SERVER['DOCUMENT_ROOT']). In case the PHP file is outside the document root, only its directory is scanned.

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

Rotate answered 1/12, 2014 at 10:14 Comment(4)
Well that functionality is not working then. It is only picking up the .user.ini file if it exists in the current directory of the script. This is using virtual directories with IIS. It may be different when installed on apache?Oly
" If you are using Apache, use .htaccess files for the same effect."Rotate
...and if I'm not using Apache? I ended up using the main php.ini and using the [PATH=xxx] to set the configuration which has propagated to the subdirectories.Oly
For me, it only works at maximum 2 level deep of folder. #41628999 localhost/test/lv2 works but localhost/test/lv2/lv3 not working.Preshrunk

© 2022 - 2024 — McMap. All rights reserved.