加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何打开文本文件并写入它附加样式与PHP?

发布时间:2020-12-13 16:25:20 所属栏目:PHP教程 来源:网络整理
导读:如何打开一个文本文件并用php appendstyle写入它 textFile.txt //caught these variables $var1 = $_POST['string1']; $var2 = $_POST['string2']; $var3 = $_POST['string3']; $handle = fopen("textFile.txt","w"); fwrite = ("%s %s %sn",$var1,$var2,$v
如何打开一个文本文件并用php appendstyle写入它
textFile.txt

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

    $handle = fopen("textFile.txt","w");
    fwrite = ("%s %s %sn",$var1,$var2,$var3,handle);//not the way to append to textfile
fclose($handle);
要将数据附加到文件中,您需要以附加模式打开文件(请参阅 fopen):
  • ‘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.

所以在只写append模式下打开textFile.txt:

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

但您也可以使用将fopen,fwrite和fclose组合在一起的更简单的功能file_put_contents

$data = sprintf("%s %s %sn",$var3);
file_put_contents('textFile.txt',$data,FILE_APPEND);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读