Sach
Sach

Reputation: 83

Alternate to parse_ini_file php

What is the alternate to parse the config file to give the absolute path in php.

ie) I am reading the db config file like:

 $config     = parse_ini_file('../../../config.ini');  

I need to give the absolute path for the above like:

$_SERVER['DOCUMENT_ROOT'].'/<my_path>/config.ini';

I need to avoid the use of '../../' in my paths

If i use:

$config = $_SERVER['DOCUMENT_ROOT'].' /my_path/my_path/config.ini';

I get

Illegal string offset 'username'

Upvotes: 0

Views: 580

Answers (2)

Sach
Sach

Reputation: 83

Ok i got it using below:

$path = getcwd().'/mypath/config.ini'; 
$config  = parse_ini_file($path);  

Upvotes: 0

Thamaraiselvam
Thamaraiselvam

Reputation: 7080

In your Root folder of the project maintain a config.php there you can add such data. This is what followed by many PHP projects including WordPress.

For example from WordPress

define('ABSPATH', dirname(__FILE__) . '/');

They defined ABSPATH in wp-config.php and it holds absolute path of application. So you refer them in your application wherever you want,

In your case this could be like following.

parse_ini_file(ABSPATH. 'config.ini');  

Upvotes: 1

Related Questions