In this guide, we will show you how to find the location of your php.ini file.
As you probably already know, the php.ini file is a configuration file that allows you to enable and disable PHP extensions. It also allows you to configure a number of key directives.
Unfortunately, finding the location of the php.ini file can be a bit tricky at times.
Use the phpinfo() function.
The easiest way to find your php.ini file is to use the PHP function phpinfo like so:
//Use phpinfo to output information //about PHP's configuration: phpinfo();
The phpinfo function is a debugging tool that prints out information about your PHP installation.
In the output, you will find a line called “Loaded Configuration File”:
This is the PHP configuration file that has been loaded by your web server.
If you want to enable an extension or alter a configuration setting, you will need to edit this file.
As you can see in the screenshot above, I am using Windows and my php.ini file is located at:
Note how I highlighted the words “loaded by your web server” above.
I made this distinction because the command line version of PHP (PHP CLI) has its own php.ini file.
To make things even more confusing, there may be other php.ini files elsewhere on your file system.
For example, the file that my web server uses can be found inside the Apache folder. However, there is also a default configuration file located at:
When I first started out programming in PHP, I would often edit the wrong file. I would then spend the next 20 minutes scratching my head and wondering why my changes weren’t taking effect.
We’ve all been there.
Note that you should always make sure that you remove your phpinfo script from your web server after you are finished using it.
As you can see in the screenshot above, the output from phpinfo contains a lot of sensitive information about your server environment.
Finding the php.ini location via the command line.
If you are using Linux and you are looking to find the php.ini file for PHP CLI, then you can use the following command:
php -i | grep 'php.ini'
The command above tells PHP to print out its configuration values. The grep utility then searches the output for a line that contains the phrase “php.ini”.
On an Ubuntu server that I have, this command resulted in the following output:
Configuration File (php.ini) Path => /etc/php/7.2/cli Loaded Configuration File => /etc/php/7.2/cli/php.ini
As you can see, the PHP CLI configuration file on my server is located at:
This means that if I want to make changes to how PHP CLI works, I will need to modify the file above.
Use the php_ini_loaded_file function.
If you are using PHP version 5.2.4 or above, then you can also use the php_ini_loaded_file function.
This function will return the location of the php.ini file as a string:
//Get the php.ini file location as a string $iniFile = php_ini_loaded_file(); echo $iniFile;
This approach is much better than using phpinfo, as it displays far less sensitive information.
If you do happen to forget to delete the script, then at least it isn’t outputting a truck load of information about your PHP environment.