When php data is written to txt, the following error occurs: filename='users.txt ';
$ Name = trim ($ _ POST ["name"]);
$ Passwd = trim ($ _ POST ["pass"]);
$ Str = $ username. '|'. $ passwd;
$ Suc = file_write ($ str, $ filename );
Function file_write ($ flag, $ tmpfname)
{
If ($ handle = fopen ($ tmpfname, ""))
{
Fwrite ($ handle, $ flag. "\ r \ n ");
Fclose ($ handle );
Return 1;
}
Else return 0;
}
Record the name and password in txt in the format of mmm | 123456
Yyy | 123456
My code is as above. why is there always a blank line when I write for the first time, whether it is blank text or there is already data. And sometimes it cannot be written into blank text. please help me optimize it.
Reply to discussion (solution)
After testing, we did not find what you said.
Although your variable name is wrong
After testing, we did not find what you said.
Although your variable name is wrong
Dizzy, I tried it and it seems to be correct. it must be that I verified whether the name already exists...
Can I see it?
$ Logi = file ("users.txt ");
$ Size = sizeof ($ Logi );
Foreach ($ Logi as $ Key => $ Val)
{$ Data [$ Key] = explode ("|", $ Val );
}
For ($ K = 0; $ K <$ size; $ K ++)
{
$ User = $ Data [$ K] [0];
If ($ user = $ _ POST ["name"])
{
Echo "This name already exits. Please get another one .";
Break;
}
If ($ K = $ size-1)
{$ Suc = file_write ($ str, $ filename );}
}
It's easy to use file_put_contents!