PHP parse_ini_file work on URL?

2020-05-02 10:05发布

问题:

Does the PHP method parse_ini_file work on an INI file hosted in the cloud? Right now, I have a config file that sits in every single one of my App servers, which could be anywhere from 4-8 at any given time. Making a config change is brutally painful to do by hand for each server.

I've tried the following but to no avail:

$handle = fopen('https://blah.com/config.ini', 'r');
$ini = parse_ini_file($handle, true);

but I get this error:

Warning: parse_ini_file() expects parameter 1 to be a valid path, resource given

Is this even possible? Any ideas?

回答1:

fopen() is not used like that. Try file_get_contents():

$content = file_get_contents('https://blah.com/config.ini');
$ini = parse_ini_string($content, true);

This may work instead (not tested):

$ini = parse_ini_file('https://blah.com/config.ini', true);

In either case allow_url_fopen needs to be enabled in php.ini.



回答2:

Fopen returns a pointer to the file, and parse_ini_file accepts file path and opens the file itself.

You should try and use file_get_contents() to get data from the file, which you then pass to parse_ini_string() function which is the same as parse_ini_file() but it takes ini file in a string.



标签: php ini