Jump to content

Recommended Posts

Hello,

 

I'm having some trouble with parse_ini_file... Let me start off with saying that I have none/very little experience with PHP, but I'm trying to learn. Also, I have looked around on the internet and not even that could help me. :tease-01:

 

Anyway, the thing I want to achieve is:

 

There's an .ini file of which the contents I want outputted into a page on my website.

 

The .ini file is called status.ini, and has these dynamic values inside:

 


[1]
Status=none
Players=0/8
Nominated=Position 1 undecided - Position 2 undecided - Position 3 undecided

[2]
Status=CTF
Players=7/8
Nominated=Position 1 map1 - Position 2 map2 - Position 3 undecided

 

I want that to display on my webpage something like:

 

(Server 1:) Status: none - Players: 0/8 - Nominated: undecided - undecided - undecided

(Server 2:) Status: CTF - Players: 7/8 - Nominated: map1 - map2 - undecided

 

Can someone tell me how to achieve this? Or hint me into the right direction?

Link to comment
https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/
Share on other sites

// The name and path of our configuration file

$file="status.ini" ;

// Lets check if the file exists and is readable by our script

if (file_exists($file) && is_readable($file))

{

//yay... the file is readable and exists

//lets parse it into an array

$status=parse_ini_file($file);

//At this point, all the data from the ini file is

//stored in the $settings array... just for the sake of

//convenience you can print it out using

//print_r($settings);

//assign different values from the file to different variables

$status=$status["status"];

$players=$status["players"];

$nominated=$status["nominated"];

 

//echo it! or use it in any other way you wish

echo "$status $players $nominated";

}

else

{

// If the configuration file does not exist or is not readable, DIE php DIE!

die("Sorry, the $file file doesnt seem to exist or is not readable!");

}

 

 

Then it gives me this error:

 

Sorry, the status.ini file doesnt seem to exist or is not readable!

Edited by Dutch

According to the logic in the script, either the file doesn't exist, or it isn't readable. Does the script reside in the same directory as the file? If not, it's a path issue . . .

your ini file is split into sections so instead of

$status=parse_ini_file($file);

 

use

$status=parse_ini_file($file, true);

 

Look at the contents now with

echo '<pre>', print_r($status, 1), '</pre>';

I would recommend that you separate the tests for file_exists() and is_readable() into two separate conditional statements, each with its own specific error message, so that you will know why the script cannot read the file.

Still getting the same error...

 

Sorry, the status.ini file doesnt seem to exist or is not readable! :(

 

 

// The name and path of our configuration file
$file="status.ini" ;
// Lets check if the file exists and is readable by our script
if (file_exists($file) && is_readable($file))
{
//yay... the file is readable and exists
//lets parse it into an array
$status=parse_ini_file($file, true);
//At this point, all the data from the ini file is
//stored in the $settings array... just for the sake of
//convenience you can print it out using
//print_r($settings);
//assign different values from the file to different variables
$status=$status["status"];
$players=$status["players"];
$nominated=$status["nominated"];

//echo it! or use it in any other way you wish
echo '<pre>', print_r($status, 1), '</pre>';
}
else
{
// If the configuration file does not exist or is not readable, DIE php DIE!
die("Sorry, the $file file doesnt seem to exist or is not readable!");
}

 

Is what I currently have...

Edited by Dutch

I would recommend that you separate the tests for file_exists() and is_readable() into two separate conditional statements, each with its own specific error message, so that you will know why the script cannot read the file.

 

Well, you certainly took that advice ::)

Well, you certainly took that advice ::)

 

Well, I tried, but got a Template Parse Error on line 177.

 

 

175: // Lets check if the file exists and is readable by our script

176: if (file_exists($file)

177: if (is_readable($file))

178: {

 

I'll assume I'm doing it wrong though. :P

Edited by Dutch

As I said earlier . . .

 

According to the logic in the script, either the file doesn't exist, or it isn't readable. Does the script reside in the same directory as the file? If not, it's a path issue . . .

As I said earlier . . .

 

It's an external hosted file, but I'm leaving the path out of my posts for security reasons. :P

 

In my script it actually says:

 

 

// The name and path of our configuration file

$file="http://www.domain.com/status.ini" ;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.