PHP 将字符串添加到文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12335885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:22:33  来源:igfitidea点击:

PHP Add string to text file

php

提问by CJ Sculti

Possible Duplicate:
writing to a text file in php

可能的重复:
在 php 中写入文本文件

Say I wanted to add the string $ip to the text file ip.txt in the same directory. I am currently using file_get_contentsto read from the text file.

假设我想将字符串 $ip 添加到同一目录中的文本文件 ip.txt 中。我目前正在使用file_get_contents从文本文件中读取。

回答by DavChana

You could use file_put_contents

你可以用 file_put_contents

<?php
  $ip = "foo";
  file_put_contents("ip.txt", $ip, FILE_APPEND);
?>

FILE_APPENDwill appendthe text. Absence of this flag will cause file-overwriting.

FILE_APPEND附加文本。缺少此标志将导致文件覆盖。

回答by moonwave99

file_put_contents()with FILE_APPENDflag appends text to given file:

file_put_contents()withFILE_APPEND标志将文本附加到给定文件:

file_put_contents('filename.txt', $stringToAppend, FILE_APPEND);

回答by jeremy

file_put_contentsto put contents into a file.

file_put_contents将内容放入文件中。

if(!file_put_contents("path/to/file.txt", $ip, FILE_APPEND)){
    // failure
}