【发布时间】:2012-09-09 01:37:17
【问题描述】:
假设我想将字符串 $ip 添加到同一目录中的文本文件 ip.txt 中。我目前正在使用file_get_contents 来读取文本文件。
【问题讨论】:
标签: php
假设我想将字符串 $ip 添加到同一目录中的文本文件 ip.txt 中。我目前正在使用file_get_contents 来读取文本文件。
【问题讨论】:
标签: php
你可以使用file_put_contents
<?php
$ip = "foo";
file_put_contents("ip.txt", $ip, FILE_APPEND);
?>
FILE_APPEND 将追加文本。缺少此标志将导致文件覆盖。
【讨论】:
file_put_contents() 和 FILE_APPEND 标志将文本附加到给定文件:
file_put_contents('filename.txt', $stringToAppend, FILE_APPEND);
【讨论】:
file_put_contents 将内容放入文件中。
if(!file_put_contents("path/to/file.txt", $ip, FILE_APPEND)){
// failure
}
【讨论】:
Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.