Php data is written to txt

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.