Problems with PHP parse_ini_file

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 had no luck. The code I'm trying to do is:

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

PHP is part of the include, so I tried adding $ _ SERVER ['DOCUMENT_ROOT'] to make sure it finds the configuration file.

The result of var_dump is always bool (false) .

What am I doing wrong?

EDIT:

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

+5
source share
2 answers

Whether your document you are trying to access is located in the root/cfi/config.ini folder, it is very likely that it goes beyond the $_SERVER['Document_Root']; variable $_SERVER['Document_Root']; which will look something like this:

/home/accountname/websitefolder/

Your ini file sounds as if it is in a folder

/home/accountname/root/cfi/

This means that you will not achieve this. You can debug if the address is correct:

 if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/cfi/config.ini")){ print fileperms($_SERVER['DOCUMENT_ROOT'] . "/cfi/config.ini"); } else { die("does not exist at this location"); } 

The fileperms function will revoke access permissions for the file, which may prevent PHP from reading if the file is in the right place.

ini File structure:

In addition, you will need to confirm that your ini file is the correct structure, the ini file must be a valid ini structure, such as the php.ini file, see http://php.net/manual/en/function.parse-ini -file.php

(edit: in favor of editing BoltClock, I want to keep the above statement regarding the structure of the INI file, since this was actually the cause of the OP problem, however I reformulated it. Thank you)

Additionally:

Tracking errors is vital to finding out why everything went wrong: From this post on SO - How do I register errors and warnings in a file? - and from the wise advice of Fred-ii-, anyone can add the following to the beginning of their file for some feedback with the error:

 error_reporting(E_ALL); ini_set('display_errors', 1); ini_set("log_errors", 1); ini_set("error_log", "/tmp/php-error.log"); 

This will look overkill for those who know, but the code above will be:

 report all errors display these errors to the browser log these errors to the log file set where the log file lives. 

Mapping and registering files is not required, but I put both so people can choose their method. Error display in the browser should be disabled using always on production servers.

Now you have a log file with many errors in it, you need your (S) FTP program to search the folder for it, the above /tmp/ folder is NOT in the domain folder of your website, but next to it, so you need (S) FTP to access it. Log in, find the folder and file, and download it. Open it with a text editor and it will give you your mistakes. Delete the file from the server and a new one will be generated when new errors occur.

+3
source

I'm not sure if this caused your general problem, but I think DOCUMENT_ROOT does not include trailing slash, so you need to add it before "cfi".

You can check:

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

Source: https://habr.com/ru/post/1214735/


All Articles