TDSii
TDSii

Reputation: 1725

php.ini path inside code

To get php.ini path i simply run

<?php
phpinfo();
?>

what is the way to get the php.ini path to show to the user. without showing the whole phpinfo file.

Upvotes: 2

Views: 359

Answers (2)

jesse
jesse

Reputation: 470

If you have PHP 5.2.4 or later, you can simply use the php_ini_loaded_file() method which returns the path as a string.

If you don't have that version, here's one way.

ob_start();
phpinfo(INFO_GENERAL);
$data = ob_get_contents();
ob_end_clean();

$lines = explode("\n", $data);
foreach($lines as $line){
    list($name, $value) = explode("=>", $line);
    if (trim($name) == 'Loaded Configuration File') break;
}    
echo $name . ' - ' . $value."\n";

That simply prints:

Loaded Configuration File - /etc/php5/cli/php.ini

Of course you could use a regex match or something fancier like that if you wanted to.

Upvotes: 1

bensiu
bensiu

Reputation: 25604

phpinfo(INFO_GENERAL) would be smaller

https://www.php.net/manual/en/function.phpinfo.php

Upvotes: 3

Related Questions