在新行中写入

问题描述 投票:0回答:1

[尝试将我的表单输入写入data.txt文件。我希望每次有人注册时都用新行写出来。试图使用.PHP-EOL,但似乎在每次输入后都未添加任何新行!

<?php
 $path = 'data.txt';
 if (isset($_POST['firstname']) && isset($_POST['country']) && isset($_POST['email'])) {
    $fh = fopen($path,"a+");
    $string = $_POST['firstname'].' - '.$_POST['country'].' - '.$_POST['email'] .PHP_EOL;
    fwrite($fh,$string); // Write information to the file
    fclose($fh); // Close the file
 }
?>
php fwrite
1个回答
0
投票

我有工作了!

在开头而不是结尾处使用PHP_EOL。

这里是代码:

<?php
 $path = 'data.txt';
 if (isset($_POST['firstname']) && isset($_POST['country']) && isset($_POST['email'])) {
    $fh = fopen($path,"a+");
    $string = PHP_EOL .$_POST['firstname'].' - '.$_POST['country'].' - '.$_POST['email'];
    fwrite($fh,$string); // Write information to the file
    fclose($fh); // Close the file
 }
?>
© www.soinside.com 2019 - 2024. All rights reserved.