【问题标题】:How to add line to the file without delete如何在不删除的情况下向文件添加行
【发布时间】:2013-07-12 21:17:48
【问题描述】:

我想在文件末尾写一行。

我的代码是:

$myFile = "serialkey.txt";
$fh = fopen($myFile, 'w');
$space = "\r\n";

$stringData = "my new data";
fwrite($fh, $stringData.$space);

fclose($fh);

但是当我使用此代码时,它删除了所有文件并替换了“我的新数据”,我希望它不会删除我的文件并将我的数据附加到它。

【问题讨论】:

    标签: php file append line


    【解决方案1】:

    您已将其设置为写入而不是追加

    $fh = fopen($myFile, 'w');
    

    应该是

    $fh = fopen($myFile, 'a');
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      要插入文本而不覆盖文件末尾,您必须打开它以进行附加 (a+ rather than w)

      $fh = fopen($myFile, 'a+');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-11
        • 2011-06-03
        • 1970-01-01
        • 2021-08-01
        • 1970-01-01
        • 2020-09-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多