Problems with PHP data written to TXT

Source: Internet
Author: User
$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, "a"))
{
Fwrite ($handle, $flag. " \ r \ n ");
Fclose ($handle);
return 1;
}
else return 0;
}
Record name and password on TXT, format is mmm|123456
yyy|123456
My code above, why the first write always empty a line, whether it is blank text or already have data. And sometimes writing blank text, can not succeed, ask the great God help me to optimize under


Reply to discussion (solution)

Tested and did not find what you were saying about the phenomenon
Even though your variable name is wrong.

Tested and did not find what you were saying about the phenomenon
Even though your variable name is wrong.
Dizzy, I tried to seem to be right, it must be I verify that the name is already present in this part of the wrong ...
Can the owner help me to see?
$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);}
}

How easy it is to use file_put_contents () directly!

  • 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.