n00bl3z Posted July 14, 2011 Share Posted July 14, 2011 Hey forums; So basically I have a PHP file (checkIP.php) that checks the IPs posted through another script against a list of IPs in a text file; I can read a text file line-by-line just fine, using "fgets()" and handles, but when I try to add the variables to an array list, it only adds the last IP to the arraylist. I'm using this code to set the array list. $ipList = array($buffer); the script i'm using to get the text line-by-line is: $handle = @fopen("./IPs.txt", "r"); if ($handle) { while (($buffer = fgets($handle, 4096)) !== false) { echo $buffer; } if (!feof($handle)) { echo "Error: unexpected fgets() fail\n"; } fclose($handle); } I'm sorry if this is a newbie question. Quote Link to comment https://forums.phpfreaks.com/topic/241939-problem-with-an-array/ Share on other sites More sharing options...
AyKay47 Posted July 14, 2011 Share Posted July 14, 2011 not sure how you are inputing the lines from fgets into the array since its not actually in your code...but here $handle = @fopen("./IPs.txt", "r"); if ($handle) { while (($buffer = fgets($handle, 4096)) !== false) { echo $buffer; $ipList[] = $buffer } if (!feof($handle)) { echo "Error: unexpected fgets() fail\n"; } fclose($handle); } print_r($ipList); Quote Link to comment https://forums.phpfreaks.com/topic/241939-problem-with-an-array/#findComment-1242523 Share on other sites More sharing options...
cyberRobot Posted July 14, 2011 Share Posted July 14, 2011 It looks like you're just overwriting $buffer each time, try: while (($buffer .= fgets($handle, 4096)) !== false) { Quote Link to comment https://forums.phpfreaks.com/topic/241939-problem-with-an-array/#findComment-1242524 Share on other sites More sharing options...
jcbones Posted July 14, 2011 Share Posted July 14, 2011 Have you tried: $ipList = file('./IPS.txt'); //set each line from the file IPS.txt into an array called ipList. Quote Link to comment https://forums.phpfreaks.com/topic/241939-problem-with-an-array/#findComment-1242530 Share on other sites More sharing options...
n00bl3z Posted July 14, 2011 Author Share Posted July 14, 2011 Thank all of you for your replies; especially thanks for getting them in quick /Solved. Quote Link to comment https://forums.phpfreaks.com/topic/241939-problem-with-an-array/#findComment-1242553 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.