Dutch Posted October 31, 2012 Share Posted October 31, 2012 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. 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? Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/ Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2012 Share Posted October 31, 2012 Where are you stuck? Post your code, any errors, and describe what it's doing/not doing that it shouldn't/should be doing. Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388874 Share on other sites More sharing options...
Dutch Posted October 31, 2012 Author Share Posted October 31, 2012 (edited) // 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 October 31, 2012 by Dutch Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388876 Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2012 Share Posted October 31, 2012 Perhaps you can post it without all that bbcode? It's pretty much unreadable. Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388879 Share on other sites More sharing options...
Dutch Posted October 31, 2012 Author Share Posted October 31, 2012 Perhaps you can post it without all that bbcode? It's pretty much unreadable. Done. I don't know why it showed all the BBCode. Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388883 Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2012 Share Posted October 31, 2012 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 . . . Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388884 Share on other sites More sharing options...
Barand Posted October 31, 2012 Share Posted October 31, 2012 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388886 Share on other sites More sharing options...
PFMaBiSmAd Posted October 31, 2012 Share Posted October 31, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388889 Share on other sites More sharing options...
Dutch Posted October 31, 2012 Author Share Posted October 31, 2012 (edited) 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 October 31, 2012 by Dutch Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388898 Share on other sites More sharing options...
Barand Posted October 31, 2012 Share Posted October 31, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388899 Share on other sites More sharing options...
Dutch Posted October 31, 2012 Author Share Posted October 31, 2012 (edited) 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. Edited October 31, 2012 by Dutch Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388901 Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2012 Share Posted October 31, 2012 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 . . . Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388902 Share on other sites More sharing options...
Dutch Posted October 31, 2012 Author Share Posted October 31, 2012 As I said earlier . . . It's an external hosted file, but I'm leaving the path out of my posts for security reasons. In my script it actually says: // The name and path of our configuration file $file="http://www.domain.com/status.ini" ; Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388903 Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2012 Share Posted October 31, 2012 (edited) Well then, there's your problem. $file is a URL string. Edited October 31, 2012 by Pikachu2000 Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388904 Share on other sites More sharing options...
Dutch Posted October 31, 2012 Author Share Posted October 31, 2012 Well then, there's your problem. $file is a URL string. Does it not work at all with external files then? Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1388995 Share on other sites More sharing options...
Barand Posted October 31, 2012 Share Posted October 31, 2012 You could try file_get_contents() + parse_ini_string() Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1389009 Share on other sites More sharing options...
Dutch Posted October 31, 2012 Author Share Posted October 31, 2012 You could try file_get_contents() + parse_ini_string() Where in the script would I put those? Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1389015 Share on other sites More sharing options...
Barand Posted October 31, 2012 Share Posted October 31, 2012 $status=parse_ini_file($file, true); becomes $status=parse_ini_string(file_get_contents($file), true); Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1389018 Share on other sites More sharing options...
Dutch Posted October 31, 2012 Author Share Posted October 31, 2012 Still getting the same error. Sorry, the status.ini file doesnt seem to exist or is not readable! Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1389021 Share on other sites More sharing options...
Barand Posted October 31, 2012 Share Posted October 31, 2012 Not a lot that we can do about that. Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1389106 Share on other sites More sharing options...
PFMaBiSmAd Posted October 31, 2012 Share Posted October 31, 2012 The two functions - file_exists() and is_readable() DON'T work with the HTTP protocol. You can simply use file_get_contents() and then test if the result was a false value or not. Quote Link to comment https://forums.phpfreaks.com/topic/270096-need-some-help-with-parse_ini_file/#findComment-1389107 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.