Problem reading a file after creating and saving data into it

Hello everyone, im using ActionScript and PHP to do some simple/basic file saving into my server.

        $file = fopen("users.ini","a");
        fwrite($file,"user=".$UserTXT.
        "\ntotal=".$_POST["totnum"].
        "\nfilter=".$_POST["ffilter"].
        "\ntext=".$_POST["ttext"].
        "\nlocal=".$_POST["tolocal"].
        "\nremote=".$_POST["toremote"].
        "\n");
        fclose($file);

        print "done=saveduser";
        print "&msg=".$UserTXT." was saved successfully";

this above is my saving into file that actually works pretty well. using \n to save into new line.
the document comes out sorted and everything is working.

the real problem for me, is reading this.

i dont know why, but the next function returns also “false”

function CheckIfUserExists($user){
    if (!file_exists("users.ini")){
        return false;  
    }
    $file = fopen("users.ini","r");

while(!feof($file)){
    $tmp = fgets($file);
    if ($tmp == "user=".$user){ //this line doesnt work? 
        fclose($file);
        return true;
    }
}
fclose($file);
return false;
}

please help me solve this “if ($tmp == “user=”.$user)” line.
i think im doing something wrong, but i cant figure it out.

I am guessing you solved this but,

You don’t need to read the whole file. You are adding the user to the first line.

$file = fopen("users.ini","r"); 
$userStr = fgets($file); 
if (trim($userStr) == "user={$user}")
    return true;
return false;

Notice I trimmed the string in the file, and I am only interested in the first line.

i see what you suggesting. but in this file, i got multiply users inside, all i want to know, if if the current user exists by reading whole file.

user=someuser
someSettings=true
user=otheruser
someSettings=true
user=thisuser
someSettings=false
and so on.

SOLVED.

fwrite($file,"user=".$UserTXT.
        "\ntotal=".$_POST["totnum"].
        "\nfilter=".$_POST["ffilter"].
        "\ntext=".$_POST["ttext"].
        "\nlocal=".$_POST["tolocal"].
        "\nremote=".$_POST["toremote"].
        "\n");

in this line, i generate one more character ("\n") the new line

wich cannot be read in same way like:
if ($tmp == “user=”.$user){…

all i need to do, is to remove the last character from $tmp string using
$tmp = substr($tmp,0,-1); //i do it right before the IF function…

thanks.

You mean like I said above? That’s what trim does.

yes. i didnt seen the TRIM you puted on.
i guess there was a simple way after all.

thanks :slight_smile:

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service