During PHP’s runtime, the php.ini configuration file is essential for adjusting environment settings and controlling system behavior. In addition to the main php.ini file, PHP may also scan and load other configuration files, which might contain extra settings or be used by specific modules. For developers, it’s sometimes necessary to confirm which configuration files are loaded in the current PHP environment.
In the command line environment, we can use PHP’s built-in function php_ini_scanned_files to retrieve the list of all configuration files that PHP has scanned. This function returns an array of file paths, indicating all additional configuration files in use by PHP.
php_ini_scanned_files is a built-in PHP function that retrieves the list of configuration files scanned in the current PHP environment. These configuration files are usually defined by PHP’s scan_dir and are used for extensions or additional settings. Typically, during PHP configuration, multiple directories are specified for scanning extra configuration files, allowing the distribution of configurations across multiple files for easier management.
The function is defined as follows:
<span><span><span class="hljs-keyword">array</span></span><span> </span><span><span class="hljs-title function_ invoke__">php_ini_scanned_files</span></span><span> ( </span><span><span class="hljs-keyword">void</span></span><span> )
</span></span>
It takes no arguments and returns an array containing the paths of all configuration files scanned during PHP initialization.
To use php_ini_scanned_files in the command line, you simply need to execute a PHP script. Assuming you already have a PHP environment set up and can run PHP scripts from the command line:
First, create a PHP file named scan_ini.php and use the php_ini_scanned_files function inside it to retrieve the scanned configuration files:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Get the list of scanned configuration files</span></span><span>
</span><span><span class="hljs-variable">$scannedFiles</span></span><span> = </span><span><span class="hljs-title function_ invoke__">php_ini_scanned_files</span></span><span>();
<p></span>// Check if any files were scanned<br>
if ($scannedFiles) {<br>
echo "Scanned configuration files:\n";<br>
foreach ($scannedFiles as $file) {<br>
echo $file . "\n";<br>
}<br>
} else {<br>
echo "No additional configuration files were scanned.\n";<br>
}<br>
?><br>
</span>
Navigate to the directory where you saved the scan_ini.php file and run the following command:
<span><span>php scan_ini.php
</span></span>
PHP will then execute the script and output the paths of all scanned configuration files. If no additional configuration files were scanned, the script will display a corresponding message.
Debugging configuration issues: When encountering PHP-related configuration errors, you may need to confirm whether PHP has loaded a specific configuration file. Using php_ini_scanned_files can help you verify if the expected file is loaded.
Multi-environment management: In different development, testing, and production environments, you might use different configuration files. This function allows you to conveniently check which configuration files are currently loaded in a given environment.
Extension configuration files: Some PHP extensions may include their own configuration files in the scan list. This function lets you check whether those extension configuration files have been loaded.
php_ini_loaded_file(): Returns the path to the main php.ini file currently in use. Useful for verifying whether PHP is using the expected primary configuration file.
get_cfg_var(): Retrieves the value of a PHP configuration variable. It can be used to check the current values of specific configuration settings.
php_ini_scanned_files is a highly practical PHP function, especially for debugging and verifying configuration files in a command line environment. It helps developers quickly identify all additional configuration files loaded by PHP, ensuring that system behavior matches expectations.
Hopefully, this article helps you better understand how to use php_ini_scanned_files to inspect PHP configuration files and resolve related issues when needed.