php - 如何清除准备好进行 fwrite 的文本文件

标签 php fwrite

我有一个正在用 fwrite 写入的文本文件,我该如何删除这个文本文件的所有内容,以便我可以重新写入它。我试图找到另一个函数,但没有成功。

我正在使用的示例代码,我想在输入此信息之前清除它:

$string = ', {"key": "'.$info['Name'].'", "value": "'.$info['Name'].'"}';
    $fp = fopen('data_old.txt', 'a');
    fwrite($fp, $string);
    fclose($fp);

最佳答案

如果您查看 fopen 的 PHP 文档,您将在第二个参数中看到可用的“模式”列表。您正在传递 "a",这意味着追加。你想传递 "w",意思是“写”。

'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.

'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

关于php - 如何清除准备好进行 fwrite 的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10422990/

相关文章:

php - 为内容创建随机生成的 url

javascript - 在文本框中显示 ajax 结果时出现问题

PHPBB 与 wordpress

c# - 通过 PHP REST api 使用 C# 进行安全身份验证和请求

C fwrite() 写入的字符数量加倍

c - 使用 fread() 从二进制文件读取显示附加字符

c - fwite/putc 是如何写入磁盘的?

javascript - 将 PHP 整数传递给 JavaScript

c - 如何正确使用fread()读取指定长度的内容(C语言)

C++ fwrite() 写的比预期的多