Php 不';无法正确写入文件中的新行
所以我有一个简单的注册表,它在file.txt中写下用户和密码。 当文件中的1行已经有用户和密码时,每一次注册都在同一行上,并在同一时间将空的下一行放入 如果文件为空,并且写入正确,则每个注册都位于新行。 以下是我使用的代码:Php 不';无法正确写入文件中的新行,php,Php,所以我有一个简单的注册表,它在file.txt中写下用户和密码。 当文件中的1行已经有用户和密码时,每一次注册都在同一行上,并在同一时间将空的下一行放入 如果文件为空,并且写入正确,则每个注册都位于新行。 以下是我使用的代码: if(!$error) { $input = $username . '|' . $pass ."\n"; file_put_contents('users/file.txt', $input, FILE_APPEND);
if(!$error) {
$input = $username . '|' . $pass ."\n";
file_put_contents('users/file.txt', $input, FILE_APPEND);
mkdir('users/'. $username);
header('Location: index.php');
exit;
}
p、 我为我的英语感到抱歉。像这样试试看
if(!$error) {
$input = $username . '|' . $pass ."\n";
$fh = fopen("users/file.txt", 'a') or die("can't open file");
fwrite($fh, $input);
fclose($fh);
header('Location: index.php');
exit;
}
也许这对你有好处:
if(!$error) {
if (strlen(file_get_contents('users/file.txt')) > 1){
$input = "\n" . $username . '|' . $pass;
} else {
$input = $username . '|' . $pass;
}
file_put_contents('users/file.txt', $input, FILE_APPEND);
mkdir('users/'. $username);
header('Location: index.php');
exit;
}
可能重复:可能的解决方案:
\r\n
而不是\n
我尝试过,结果是一样的。我不明白这是为什么。如果文件已经有第一行,第二行为空,则正确写入。但是如果只有第一行会写在第一行?你在Windows上并试图在记事本中打开文件.txt吗?是的,我在Windows的localhost上。如果文件中没有空的第二行写在同一行上,我就使用记事本+。只有当我已经有一行并且在第一行之后没有第二行是空的时才会发生这种情况。我想我会用空的第二行保存文件。我找不到其他解决办法。@Goro我很高兴能帮上忙