How to Find the PHP.Ini For PHP-Cli

How can I find the php.ini file used by the command line?

Just run php --ini and look for Loaded Configuration File in the output for the location of php.ini used by your CLI.

where can I find the php.ini for php-cli

Just ask PHP:

php -i |grep php\.ini

Where can I find php.ini?

The best way to find this is:

Create a PHP (.php) file and add the following code:

<?php phpinfo(); ?>

and open it in a browser. It will show the file which is actually being read!

Updates by the OP:

  1. The previously accepted answer is likely to be faster and more convenient for you, but it is not always correct. See comments on that answer.
  2. Please also note the more convenient alternative <?php echo php_ini_loaded_file(); ?> mentioned in this answer.

How to change the path to php.ini in PHP CLI version

Per http://php.net/configuration.file:

php.ini is searched for in these locations (in order):

  • SAPI module specific location (PHPIniDir directive in Apache 2, -c command line option in CGI and CLI, php_ini parameter in NSAPI, PHP_INI_PATH environment variable in THTTPD)
  • The PHPRC environment variable. Before PHP 5.2.0, this was checked after the registry key mentioned below.
  • As of PHP 5.2.0, the location of the php.ini file can be set for different versions of PHP. The following registry keys are examined in order:
    • [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y.z], [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y] and [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x], where x, y and z mean the PHP major, minor and release versions. If there is a value for IniFilePath in any of these keys, the first one found will be used as the location of the php.ini (Windows only).
    • [HKEY_LOCAL_MACHINE\SOFTWARE\PHP], value of IniFilePath (Windows only).
  • Current working directory (except CLI).
  • The web server's directory (for SAPI modules), or directory of PHP (otherwise in Windows).
  • Windows directory (C:\windows or C:\winnt) (for Windows), or --with-config-file-path compile time option.

For CLI, your best bet is probably either to set the $PHPRC environment variable for the account that will be executing scripts, or recompile PHP with a different --with-config-file-path configuration setting.

You can also override the php.ini search dir on a per-execution basis by specifying the -c option when invoking PHP:


> php --help
Usage: php [options] [-f] [--] [args...]
...
-c | Look for php.ini file in this directory

How can I know which 'php.ini' file is used?

You can use php_ini_loaded_file().

Taken from php.net:

$inipath = php_ini_loaded_file();
if ($inipath) {
echo 'Loaded php.ini: ' . $inipath;
} else {
echo 'A php.ini file is not loaded';
}

You may also want to check php_ini_scanned_files().

Also, you should note that if you run a PHP script from CLI, it's possible that a different php.ini file will be used than if a server (e.g., nginx or Apache) runs it.

Other options:

  • php -i|grep 'php.ini'
  • create info.php that contains <?php phpinfo(); in the webroot, and run it in your browser

php.ini different from php info (cli only)

You cannot set max_execution_time for CLI. See the manual, in the section "Overridden php.ini directives":

PHP in a shell environment tends to be used for a much more diverse range of purposes than typical Web-based scripts, and as these can be very long-running, the maximum execution time is set to unlimited.

In other words, no matter what you set for max_execution_time in php.ini, CLI wil override it to be unlimited.

If you want to limit the execution time for a script, you can use the set_time_limit() function in your script itself.



Related Topics



Leave a reply



Submit