如何打开文本文件并用php将其写入追加样式? [英] How to open textfile and write to it append-style with php?

查看:127
本文介绍了如何打开文本文件并用php将其写入追加样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打开文本文件并使用php appendstyle对其进行写入

How do you open a textfile and write to it with php appendstyle

    textFile.txt

        //caught these variables
        $var1 = $_POST['string1'];
        $var2 = $_POST['string2'];
        $var3 = $_POST['string3'];

    $handle = fopen("textFile.txt", "w");
    fwrite = ("%s %s %s\n", $var1, $var2, $var3, handle);//not the way to append to textfile
fclose($handle);

推荐答案

要将数据追加到文件中,您需要以追加模式打开文件(请参见

To append data to a file you would need to open the file in the append mode (see fopen):

  • 'a'
    仅开放写作;将文件指针放在文件末尾.如果文件不存在,请尝试创建它.
  • 'a +'
    开放供阅读和写作;将文件指针放在文件末尾.如果文件不存在,请尝试创建它.
  • 'a'
    Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
  • 'a+'
    Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

因此要在仅写入附加模式下打开 textFile.txt :

fopen("textFile.txt", "a")

但是您也可以使用更简单的功能 file_put_contents ,该功能结合了fopenfwritefclose的一项功能:

But you can also use the simpler function file_put_contents that combines fopen, fwrite and fclose in one function:

$data = sprintf("%s %s %s\n", $var1, $var2, $var3);
file_put_contents('textFile.txt', $data, FILE_APPEND);

这篇关于如何打开文本文件并用php将其写入追加样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆