'PHP parse_ini_file problems

I have a config.ini file that looks like this:

[database]
username = user1
password = pass1
dbname = db1

And I want to parse it in PHP, but I'm having no luck. The code I'm trying is:

$inifile = parse_ini_file($_SERVER['DOCUMENT_ROOT'] . "/cfi/config.ini", true);
var_dump($inifile);

The PHP is part of an include, thats why I tried adding $_SERVER['DOCUMENT_ROOT'] to make sure it was finding the config file.

The result of the var_dump is always bool(false).

What am I doing wrong?

EDIT:

I have added the / and it now definitely points to the config file. But there is still a result of bool(false)

php


Solution 1:[1]

I'm not sure if it's causing your overall issue, but I think DOCUMENT_ROOT does not include a trailing slash, so you'll need to add one to before "cfi".

You can check with:

echo $_SERVER['DOCUMENT_ROOT'] . "cfi/config.ini";

Solution 2:[2]

I had the same problem with my code. After trying with some failures I noticed a special char ('^') in my password was cause of the error. It was fixed by putting the values inside double quotes ("").

For eg:

[database]
username = "user1"
password = "pass1"
dbname = "db1"

Hope this may help someone like me.

Solution 3:[3]

My problem was that I has a variable no. Changing variable name solved my problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Funk Forty Niner
Solution 2 Naseer
Solution 3