I have a file in which I store user information. The filename is johndoe (with no extension):
johndoe
John,Doe,JohnDoe,[email protected],abcd1234
I am trying to get John, Doe and JohnDoe from the file in this way:
index.php
$filename = explode("/", $_SERVER["PHP_SELF"])[2];
$userpath = "http://192.168.0.1/member/" . $filename;
$userfile = fopen($userpath,"r");
$username = explode(",",fgets($userfile))[2];
$firstname = explode(",",fgets($userfile))[0];
$lastname = explode(",",fgets($userfile))[1];
where $_SERVER["PHP_SELF"] currently returns /member/johndoe/index.php.
When I print the array explode(",",fgets($userfile)) using print_r(), I get:
Array ( [0] => John [1] => Doe [2] => JohnDoe [3] => [email protected] [4] => abcd1234 )
However, I can't access any of the array elements. For example, echoing explode(",",fgets($userfile))[2] throws an error:
Notice: Undefined offset: 2 in 192.168.0.1/member/johndoe/index.php on line 8
fgetswill not reset the file, it will try to read the next string. Which will be empty on the second read.fgets()multiple times.