This is a guide on 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.
However, finding the location of php.ini file can be a tad 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 will output information about your PHP installation. In this output, you will find a line called Loaded Configuration File:
This is the PHP configuration file that has been loaded by your web server. Therefore, this is the file that you will need to edit if you want to enable an extension or tweak the upload size limit. 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) will have 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 developing with PHP, I would often end up editing 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 PHP 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 dump out its configuration values. The grep utility then searches the resulting 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, then 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 might be preferable to using phpinfo, as it displays far less sensitive information about your installation. 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.
Hopefully, you found this guide useful!